How to save a plot with the name of the file

46 ビュー (過去 30 日間)
alberto caracciolo
alberto caracciolo 2019 年 2 月 14 日
コメント済み: Mark Sherstan 2019 年 3 月 1 日
Hi,
I'm a beginner on Matlab.
I have a script that read an excel table, does some operations and plots 3 different figures.
What I would like to do is to save each figure with a different name, made of the script's name + _Fo (for example). So if scripts are named file1, file2 and file 3, I want figure 1 to be named file1_Fo, figure 2 as file2_Mn and figure 3 as file3_Ni. The name of the script and the name of the excel table are the same!
How could I do that?
I've tried with something like that
saveas(fig(1), sprintf('_Fo.jpg', 1))
But i don't know how to put the name of the file automatically in front of _Fo.
Thank you
Alberto

回答 (2 件)

Mark Sherstan
Mark Sherstan 2019 年 2 月 14 日
編集済み: Mark Sherstan 2019 年 2 月 14 日
Use the strcat function. For example...
x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,strcat('file',num2str(ii),'_Fo.jpg')) % Where ii would be the number you want (e.g. you could make a loop so it is not hard coded).
  2 件のコメント
alberto caracciolo
alberto caracciolo 2019 年 2 月 19 日
Thank you but I don't understand it!
Let's say that I have a script (e.g called MyTest) that produces 3 different figures.
How do I save them automatically with a name like MyTest_Fo, MyTest_Ni and MyTest_Mn?
I would like a way to automatically get the script's name + _Fo, without manually type the script's name!
I don't know if you see what I mean!
Cheers!
Mark Sherstan
Mark Sherstan 2019 年 3 月 1 日
See the answer below!

サインインしてコメントする。


Stephen23
Stephen23 2019 年 2 月 19 日
Use the mfilename function:
M = mfilename();
C = {'Fo','Mn','Ni'};
for k = 1:3
F = sprintf('%s%d_%s.jpg',M,k,C{k})
saveas(fig(1),F)
end

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by