How to convert easily *.mat files to *.m files

146 views (last 30 days)
I tried to do my own routine (see below) but I am not totally satisfied. Does someone known a smarter solution ?
A built-in matlab function would be the best.
function mat2m(calib_name)
% Convert file_name.mat to file_name.m
% Copy variable (in upper case) names and values as follows:
% A = "value of A"; or A = ["values of A"];
path_name = which(calib_name); % get loading file path
path_name = strrep(path_name,'.mat','.m'); % create output file path
% try/catch for .mat loading
try
load(calib_name)
error=0;
catch
disp(['Error loading ',calib_name])
error=1;
end
if error == 0
fid = fopen(path_name,'w'); % open file and get file id
dd=whos; % get DD list (loading calib + working variables as dd)
for j=1:length(dd) % parse DD list
if strcmp(lower(dd(j,1).name(1)),dd(j,1).name(1))
% check if first letter is lower case
% lower case = working variables (not used)
else
fprintf(fid,dd(j,1).name); % write one var name in file
siz=eval(['size(',dd(j,1).name,')']); % check var size
if siz(1,1)==1 % Xdim = 1
if siz(1,2)==1 % Ydim = 1
%0-D
fprintf(fid,' = '); % write start of the assignation
fprintf(fid,num2str(eval(dd(j,1).name))); % write value of the variable
fprintf(fid,';'); % write end of the assignation
else % Ydim > 1 (siz(1,2)>1)
%1-D
fprintf(fid,' = ['); % write start of the assignation
for k=1:siz(1,2)-1
fprintf(fid,num2str(eval([dd(j,1).name,'(k)'])));
fprintf(fid,',');
end
fprintf(fid,num2str(eval([dd(j,1).name,'(siz(1,2))'])));
fprintf(fid,'];'); % write end of the assignation
end
else % Xdim > 1 (siz(1,1)>1)
fprintf(fid,' = ['); % write start of the assignation
if siz(1,2)>1 %2-D % specific case of 2D matrix
for k=1:siz(1,1)
for l=1:siz(1,2)
fprintf(fid,num2str(eval([dd(j,1).name,'(k,l)'])));
if l~=siz(1,2)
fprintf(fid,',');
end
end
if k~=siz(1,1)
fprintf(fid,';');
end
end
else
%1-D
for k=1:siz(1,1)-1
fprintf(fid,num2str(eval([dd(j,1).name,'(k)'])));
fprintf(fid,';');
end
fprintf(fid,num2str(eval([dd(j,1).name,'(siz(1,1))'])));
end
fprintf(fid,'];'); % write end of the assignation
end
fprintf(fid,'\n'); % write end of the line for next var
eval(strcat('clear(''',dd(j,1).name,''')')); % clear var from workspace
end
end
fclose(fid); % close file with its id
else
% Do nothing
end
end
  4 Comments
Valery Carpentier
Valery Carpentier on 21 Jun 2012
Because m-file is readable without Matlab

Sign in to comment.

Accepted Answer

Teja Muppirala
Teja Muppirala on 21 Jun 2012
Do you happen to have Simulink? There is the "Simulink.savevars" command that will write variables into a readable MATLAB script.
  2 Comments
Valery Carpentier
Valery Carpentier on 21 Jun 2012
Perfect.
The command works fine in R2011b.
Simulink.savevars was not in Matlab R2007b but we will migrate to R2011b.
Sean de Wolski
Sean de Wolski on 1 Aug 2016
And in more recent releases:
matlab.io.saveVariablesToScript

Sign in to comment.

More Answers (3)

Daniel Shub
Daniel Shub on 21 Jun 2012
I am guessing you want a human readable m file, but since you didn't specify that ...
function mat2m(fname)
fid(1) = fopen([fname, '.mat']);
fid(2) = fopen([fname, '.m'], 'w');
fprintf(fid(2), '%s\n', 'fid = fopen(''temp.mat'', ''w'');');
fprintf(fid(2), '%s', 'fwrite(fid, [');
fprintf(fid(2), '%d\n', fread(fid(1))');
fprintf(fid(2), '%s\n', ']);');
fprintf(fid(2), '%s\n', 'evalin(''base'', ''load temp'');');
fclose(fid(1));
fclose(fid(2));
end
With the above code
mat2m('myfile');
myfile;
where myfile.mat exists should do behave "exactly" like
load myfile

Walter Roberson
Walter Roberson on 21 Jun 2012
Please avoid using eval(). Instead of load() into the workspace, assign the output of load() to a variable, and use fieldnames() of that variable instead of whos(), and use dynamic structure fieldnames.

DILIP KUMAR GARG
DILIP KUMAR GARG on 22 Oct 2021
matlab.io.saveVaraiblestoScript(filename)

Products

Community Treasure Hunt

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

Start Hunting!