How can i generate .mat file from eclipse (java) ?
Show older comments
Heya,
How do I create a new .mat file and write data into it from java (Eclipse)? Also I need to load this newly created .mat file onto Matlab
Accepted Answer
More Answers (2)
Florian Enner
on 1 Nov 2018
Edited: Florian Enner
on 23 Feb 2024
0 votes
Text based formats like CSV come with a lot of parsing overhead. Loading a 100 MB CSV file can take a long time and resources, while loading a comparable MAT file can be almost instant. CSV also has issues mapping multi-dimensional matrices and other complex data like nested structures.
Coming up with a custom binary format using fget/fopen is only worth it for extremely simple use cases. Unlike suggested in the previous answer, most of the features in the (Level 5) MAT-File format are well documented and have remained mostly the same since 1996. Since the format is actually quite popular, there are several open source libraries that cover bindings for many different programming languages.
For Java you can use the MAT File Library (disclaimer: I'm the main author). There is also the MatFileRW fork of the ancient JMatIO library, but as of 2024 I don't see a point in using it anymore.
All that being said, if you really want to use a text based format for some reason, JSON would probably be your best option for the database case.
Cengiz
on 23 Feb 2024
0 votes
I try to convert .csv file to .mat file using java. I write the following piece of code. I attached the sample output .mat file. When I try to view the content of .mat file using MATLAB, I see only Time array, but when I use third parity tool like mat file reader(https://github.com/worlddatong/MatFileViewer) I see all the arrays. I could not resolve the problem. Any help would be appreciated. Regards. Cengiz EKEN
Since I can not upload a .java file, I attached it as .zip file named "CsvToMatFileExporter.zip"
public void exportToMatFile(String filePath) {
try {
// Create a list to store MLArray objects
ArrayList<MLArray> mlArrays = new ArrayList<>();
// Add each column to the list
for (Map.Entry<String, List<Double>> entry : csvData.entrySet()) {
String columnName = entry.getKey();
List<Double> columnData = entry.getValue();
// Convert List<Double> to double[]
double[] dataArray = columnData.stream().mapToDouble(Double::doubleValue).toArray();
// Create MLDouble object for each column
MLDouble mlDouble = new MLDouble(columnName, dataArray, 1);
// Add MLDouble to the list
mlArrays.add(mlDouble);
}
// Write MLArrays to MAT file
MatFileWriter matFileWriter = new MatFileWriter();
matFileWriter.write(filePath, mlArrays);
} catch (IOException e) {
e.printStackTrace();
}
}
1 Comment
Florian Enner
on 23 Feb 2024
Besides 'Time' all your variables start with numbers and can't be loaded by MATLAB because of variable naming restrictions. Fix the names and you'll be able to load the file.
>> data = load('file.mat')
Error using load
Invalid field name: '11_Velocity_X_mPERs'.
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!