MATLAB Answers

save in a folder

497 views (last 30 days)
Hi,
I want to save in a folder mat files with a changing name.
for i=1:length(ID)
Filename=[num2cell(ID(i)) '.mat'];
save('C:\Users\Documents\MATLAB\TechnicalFinal\Filename','Close')
end
I am ending up with a file called Filename.mat instead of 1.mat,2.mat,and 3.mat where 1,2,3 are the ID number in the loop. Is there a way to fix that? Best

  0 Comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 3 Mar 2013
Edited: Azzi Abdelmalek on 3 Mar 2013
use
for k=1:5
Filename=sprintf('%d.mat',k);
end
Also
save(['C:\Users\Documents\MATLAB\TechnicalFinal\' Filename],'close')
Because
filename='2.mat'
folder='C:\Users\filname'
Result
C:\Users\filname
and now
filename='2.mat'
folder=['C:\Users\' filename]
Result
C:\Users\2.mat

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Mar 2013
Follow the instructions in The FAQ. It covers changing mat filenames in a loop. It's also good to use fullfile() in addition to sprintf() to build the complete filename, as the second FAQ example shows.

  0 Comments

Sign in to comment.

Tags