How to convert easily *.mat files to *.m files
Show older comments
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
Daniel Shub
on 21 Jun 2012
So you want a function that when given the name of a mat file will create an m file. When this newly created mfile is run you want it to essentially create a new mat file which is binary equal to the original mat file?
Sean de Wolski
on 21 Jun 2012
>>why
Seriously, why do you want to do this?
Valery Carpentier
on 21 Jun 2012
Renish Ramegowda
on 28 Jul 2016
Are you still in need of this file?
Accepted Answer
More Answers (3)
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
on 21 Jun 2012
0 votes
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
on 22 Oct 2021
0 votes
matlab.io.saveVaraiblestoScript(filename)
1 Comment
Walter Roberson
on 22 Oct 2021
I think you might mean matlab.io.saveVariablesToScript https://www.mathworks.com/help/matlab/ref/matlab.io.savevariablestoscript.html
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!