How can i generate .mat file from eclipse (java) ?

8 views (last 30 days)
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

John D'Errico
John D'Errico on 29 Jul 2016
Why bother?
For example, there are many tools in MATLAB that can read an ascii based text file of some ilk. Anything from load to textread, to csvread, etc. Or you can do the reads using more basic tools like fopen and fget, etc.
So reading a file need not be done by loading a .mat file. But creating the .mat file will be more problematic, since then you need to know the proprietary format of .mat files, something that has changed over the years. Simply not worth the effort, not when there are simpler ways to solve the problem.
A virtue of using a text file (of whatever your favorite style) is that you can simply verify the contents. Did you have a problem? Just check the file in a text editor. Did it get written out properly? Easy to check.
Don't look for the solution to a difficult problems when there are simple alternatives readily available.
  1 Comment
Vinay  Sheshadri
Vinay Sheshadri on 29 Jul 2016
Edited: Vinay Sheshadri on 29 Jul 2016
My scenario : I gotta read data from database into a file format that is supported by Matlab. Then load the file containing the database data to matlab automatically and trigger Code Generation function of Matlab.
Would opting text file as the format still help ?

Sign in to comment.

More Answers (2)

Florian Enner
Florian Enner on 1 Nov 2018
Edited: Florian Enner on 23 Feb 2024
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
Cengiz on 23 Feb 2024
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
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'.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!