How do I write a .mat file (exported from the Design Data in an .sldd file) to a csv file?
6 views (last 30 days)
Show older comments
I am trying to write the contents of a .mat file to a csv file. The .mat file was generated by exporting the Design Data from a simulink data dictionary file that is used in a Simulink model. I've tried to research how to extract the data from the .mat but haven't been able to find anything significant. If you have any suggestions for strategies or functions I could use, please add a comment. Thanks.
CODE:
FileData = load('SLDD_220817.mat');
NewData=struct2cell(FileData);
csvwrite('SLDD_220817.csv', NewData);
CW OUTPUT:
Error using csvwrite (line 47)
The input cell array cannot be converted to a matrix.
Error in sldd_to_csv (line 11)
csvwrite('SLDD_220817.csv', NewData);
2 Comments
Jasmine Dhaliwal
on 12 Oct 2022
Does the "load" function part work? is it just the csvwrite not working? Hard to tell because I don't know which is line 11, I'm sorry TT. I will say also, technically csvwrite isn't recommended- try using writematrix instead!
writematrix(NewData,'wtvName.csv')
^ there's the Help Center link if you wanted to see more about this function!
Answers (1)
Yash
on 31 Jan 2025
Edited: Yash
on 31 Jan 2025
Hi Nate,
As the error says "The input cell array cannot be converted to a matrix.", the main reason is that the contents of cell array is having different data types (for example a char and a double). It is therefore unable to convert it to a matrix.
A better approach would be to convert the data to a table instead of a cell array, and write it to csv file using the "writetable" function.
Given below is the updated code:
FileData = load('SLDD_220817.mat');
NewData = struct2table(FileData);
writetable(NewData, 'SLDD_220817.csv');
For more information, refer to the following documentations:
0 Comments
See Also
Categories
Find more on Text Files 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!