Loop to change .m file to .mat file
Show older comments
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
Jan
on 15 Aug 2011
Please describe the format of the M-files. Are they functions or scripts?
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
on 16 Aug 2011
Accepted Answer
More Answers (1)
Tanaya
on 16 Aug 2011
0 votes
Categories
Find more on Workspace Variables and MAT Files 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!