toCSV method
Converts the Matrix to a CSV string and optionally saves it to a file.
This method converts the Matrix object to a CSV string, using the specified delimiter. If an output file path is provided, the CSV string will be saved to that file.
delimiter: The delimiter to use in the CSV string (default: ',').
outputFilePath: The output file path to save the CSV string (optional).
Example:
Matrix matrix = Matrix.fromList([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
String csv = matrix.toCSV(outputFilePath: 'output.csv');
print(csv);
Output:
1.0,2.0,3.0
4.0,5.0,6.0
7.0,8.0,9.0
Implementation
Future<String> toCSV({String delimiter = ',', String? outputFilePath}) async {
String csv =
map((row) => row.map((value) => value.toString()).join(delimiter))
.toList()
.join('\n');
if (outputFilePath != null) {
// Save file
fileIO.saveToFile(outputFilePath, csv);
}
return csv;
}