How can i generate .mat file from eclipse (java) ?
5 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
John D'Errico
2016 年 7 月 29 日
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.
その他の回答 (2 件)
Florian Enner
2018 年 11 月 1 日
編集済み: Florian Enner
2024 年 2 月 23 日
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.
0 件のコメント
Cengiz
2024 年 2 月 23 日
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 件のコメント
Florian Enner
2024 年 2 月 23 日
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'.
参考
カテゴリ
Help Center および File Exchange で Call Java from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!