How to name a figure file with filename in for loop
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
2018-7-31
[~, basename, ~] = fileparts(NameOfMATFile);
filename = [basename '.fig'];
savefig(filename);
0 个评论
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?
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!