Loop to change .m file to .mat file

20 views (last 30 days)
Hello
I don't know if this is possible but I am trying to create a loop that will ultimately change the file extension from .m to .mat. I can achieve this with each file individually, however I will be converting 1000's of .m file and would to automate this process.
For this problem, I have 50 .m files named ts01.m, ts02.m, ts03.m, ... ts50.m. Each file contains a matrix of size 1500x1001. The name of each matrix corresponds with the file name; the matrix in the first file is named: t1, the matrix in the second file is named: t2, and so forth, through all 50 files. Here is the code that I have written so far.
model=('testdata'); % state desired data set
cd(model) % change directory to get dta set
files=dir('*.m'); % creates a struct listing all the files in the directory
cEXT{1,1}='.mat';
for i=1:50;
%filename=files(i).name; % gives struct of file name and extension
%everypath=strcat('/untitled folder/', model, '/', filename); % gives full path incld extension
%[pathstr, name, ext]=fileparts(everypath); % breaks down path
varname=strcat('t', int2str(i)); % name of matrix (variable to be loaded)
%newname=strcat(name, num2str(i), cEXT{1,1});
newname=strcat(varname, num2str(i), cEXT{1,1});
save(newname, '-mat')
end
Included in the code are a few extra lines that i thought might be useful, but haven't been able to incorporate.
The code creates and names the .mat files but I haven't been able to get the matrix data into that file. Also, the .m files are too large to open and I don't know how to access the .m file data without opening the file, and without using the import wizard. I feel that this might be a good first step.
I am fairly new to Matlab and would greatly appreciate any suggestions or comments about this code. Thank you very much for any feedback.
Sincerely,
Tanaya
Note: I don't know how much this matters but I am using version R2011a on a MAC.
  3 Comments
Oleg Komarov
Oleg Komarov on 15 Aug 2011
You say: "Each file contains a matrix of size 1500x1001."
You mean in the file there's an assignment of the type:
t1 = [12 323 ...; 21321 32 32 etc];
Tanaya
Tanaya on 16 Aug 2011
The m-files are scripts and Oleg, you are correct, the assignment is exactly as you have described. If it makes it easier to work with, I can make the files only have the data, without the variable name, brackets, and ending semi-colon, (leaving just a set of numbers arranged as 1500x1001.)

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Aug 2011
First of all, you have to understand that .m file is a text file. It could be a m-script file or a m-function file. The .mat file is a binary file. It is used by MATLAB to save data so it is commonly referred as .mat data file.
You can't simply rename *.m file to *.mat file. That will cause the file un-usable in MATLAB. You could go through a loop. At every iteration, clear all the data in the workspace first, then run a .m file so whatever data specified in that .m file will be established in the workspace, and then you save the workspace data to a .mat file.
Your overall code flow looks good. One mistake is that "newname" won't be correct. For one iteration, newname would be 't11.mat'.
If you know your file name is ts01.m ts02.m ... and data is t1,t2,... You can do like this.
model='testdata';
cd(model);
for k=1:50
mfile=sprintf('ts%02d',k);
eval(mfile);
varname=['t',num2str(k)];
matfile=[varname,'.mat'];
save(matfile,varname);
clear(varname);
end

More Answers (1)

Tanaya
Tanaya on 16 Aug 2011
Fangjun
Thank you for your quick response. This code accomplishes exactly what I wanted. I greatly appreciate your help. Thank you so much.
Best Regards!
Tanaya

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!