Can I generate matlab code from a .mat file?

4 views (last 30 days)
Hello
I have saved the networks I created using the nntoolbox.Its is saved as a .mat file. Is it possible for me to generate code from the same?
Thank you.
  2 Comments
Jan
Jan on 17 May 2015
Edited: Jan on 17 May 2015
Please explain the purpose of this. What's wrong with loading the MAT file?
Aravinthan Ramanathan
Aravinthan Ramanathan on 3 Dec 2019
.m file is erased and we need to make changes in the code

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 17 May 2015
If you have ordinary numeric arrays that are at most 2 dimensions, then you can use mat2str(A,75) to generate a string that will rebuild the array; you can then write the string to a file. For example,
fid = fopen('mycode.m','wt');
fprintf(fid, 'A = %s;\n', mat2str(A,75));
fclose(fid);
Given any particular .mat file, code could be written to do that automatically for all numeric variables. A small extension would handle character strings.
If the code was made recursive, it would be possible to extend it to also handle struct and cell arrays of {struct, numeric, string}.
Depending on your purpose, you might be interested in the File Exchange contribution to serialize data. This has to do with constructing byte arrays that can be interpreted to rebuild the data.

Categories

Find more on File Operations 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!