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

73 ビュー (過去 30 日間)
Valery Carpentier
Valery Carpentier 2012 年 6 月 21 日
コメント済み: Walter Roberson 2021 年 10 月 22 日
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 件のコメント
Valery Carpentier
Valery Carpentier 2012 年 6 月 21 日
Because m-file is readable without Matlab
Renish Ramegowda
Renish Ramegowda 2016 年 7 月 28 日
Are you still in need of this file?

サインインしてコメントする。

採用された回答

Teja Muppirala
Teja Muppirala 2012 年 6 月 21 日
Do you happen to have Simulink? There is the "Simulink.savevars" command that will write variables into a readable MATLAB script.
  2 件のコメント
Valery Carpentier
Valery Carpentier 2012 年 6 月 21 日
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 2016 年 8 月 1 日
And in more recent releases:
matlab.io.saveVariablesToScript

サインインしてコメントする。

その他の回答 (3 件)

Daniel Shub
Daniel Shub 2012 年 6 月 21 日
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 2012 年 6 月 21 日
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 2021 年 10 月 22 日
matlab.io.saveVaraiblestoScript(filename)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by