How to name a figure file with filename in for loop

조회 수: 13 (최근 30일)
Amit Pandey
Amit Pandey 2018년 7월 31일
편집: jonas 2018년 8월 1일
i have multiple files in a folder and i want to save each figure with the filename.fig within a for loop. my mat file name are "F_BTW_0998_180702","F_BTW_0998_180703","F_BTW_0998_180704" ..etc. when i use savefig(filename). it only save with the first alphabet i.e. F.fig
thanks in advance for responses.
  댓글 수: 3
Amit Pandey
Amit Pandey 2018년 8월 1일
I am using savefig(filename(k)); all the filenames have a datestamp at the end. ex- "F_BTW_0998_180702.mat","F_BTW_0998_180703.mat","F_BTW_0998_180704.mat" etc. but the sabove savefig command saves figure as "F.fig" , "_.fig" , "B.fig" ..etc it is oly saving as one alphabet rather the full filename
jonas
jonas 2018년 8월 1일
편집: jonas 2018년 8월 1일
Yea, that's because (k) in your code grabs the letter with index 'k'. If you loop over the files and replace the name filename in each iteration, just remove (k) from filename(k) and you should be fine.
Please, next time include the code in your original submission. This could have been resolved in two minutes.

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2018년 7월 31일
[~, basename, ~] = fileparts(NameOfMATFile);
filename = [basename '.fig'];
savefig(filename);

Ankita Bansal
Ankita Bansal 2018년 7월 31일
편집: Ankita Bansal 2018년 7월 31일
Hi Amit, are you sure that file names are stored correctly in the variable "filename"? Can you send what you have tried so far?
  댓글 수: 1
Amit Pandey
Amit Pandey 2018년 8월 1일
yes, the filename stores "F_BTW_0998_180702". and i used savefig(filename(k)); where k=1:25 (total 25 files)
but it saves the figures are 1). F.fig 2). _.fig
it only saves 1 alphabet of the filename

댓글을 달려면 로그인하십시오.


Ankita Bansal
Ankita Bansal 2018년 8월 1일
Hi, you should store filenames in a cell array
like
filenames = {'F_BTW_0998_180702'; 'F_BTW_0998_180703'; 'F_BTW_0998_180704'};
this will allow you to get the full name instead of letters
after this, you can do something like this:
y = randi(10,1);
for i=1:3
y1=i*y;
plot(y1)
filename = char (filenames(i)); % savefig can not take cell class variable so convert to char
savefig(filename)
end

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by