How to save multiple MATLAB figures automatically in program?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
In my script, I may save the figures as jpg files via the code (which works):
% print(1,'-painters,'-djpeg',strcat(filename,'r','.jpg'))
I want to do the same thing, but saving my files as figs in MATLAB. That's it, but it has to work the same, which means the filename must be saved as well. This is variable with the iterations of the program.
I should also mention there are two more print commands in the original code that are identical except for the number and the colour.
採用された回答
Note: You should save using the full file name, which includes the folder path AND file name. Otherwise, you cannot use your program if your current working directory is different.
FullFileName = fullfile(FilePath, FileName)
Here's an example.
%Example
FigH = figure; %If you have other figure handles, then specify
%this when saving figures. Note: you can store
%figure handles in a cell array or gobjects
%array, in case you need to loop through the saves.
%EX: FigH{1} = figure. FigH{2} = figure.
AxH = axes;
X = 1:100;
Y = sin(X);
FileDir = pwd; %Replace with your folder
for j = 1:10
LineH = plot(AxH, X, Y*j);
FilePre = sprintf('%s-%d', 'MyFig', j);
savefig(FigH, fullfile(FileDir, [FilePre '.fig']))
print(FigH, fullfile(FileDir, [FilePre '.png']), '-r300', '-dpng')
%Changing figure color
LineH.Color = 'r';
savefig(FigH, fullfile(FileDir, [FilePre 'red.fig']))
print(FigH, fullfile(FileDir, [FilePre 'red.png']), '-r300', '-dpng')
end
9 件のコメント
I am using 'savefig', but I get the error message 'Argument must be a .fig file'. My code is currently:
savefig(1,filename);
savefig(2,filename);
savefig(3,filename);
where there are 3 figures produced, and filename is a string defined via 'strcat()' in the script. This definition of the filename is necessary, as the filename is dynamical. My other attempts at using savefig have had issues as well. Also, I 'cd' to the correct directory I want to save the files in at the top of my script, so there should be no issue here with the location. Any suggestions from here?
Steven Lord
2018 年 10 月 16 日
Does filename end with the '.fig' extension?
filename is a string defined via 'strcat()' in the script
Please post the code instead of describing, what it should do. Obviously there is an error in the code, so we must see it to find the problem.
I cannot post the code. May I ask if one can give a general example of how to save MATLAB figures via a dynamical filename that changes with changing values as the program passes through iterations? This is what I am after. Thus, the filename would need to change as certain variables in the program would change. I am easily doing this with pdf and jpeg, but nothing seems to let me do this with MATLAB figures. Also, there would be 3 figures produced for each iteration. I need to save all three.
FigH = figure;
AxH = axes;
X = 1:100;
Y = sin(X);
FileDir = pwd; %Replace with your folder
for j = 1:10
LineH = plot(AxH, X, Y*j);
FilePre = sprintf('%s-%d', 'MyFig', j);
savefig(FigH, fullfile(FileDir, [FilePre '.fig']))
print(FigH, fullfile(FileDir, [FilePre '.png']), '-r300', '-dpng')
%Changing figure color
LineH.Color = 'r';
savefig(FigH, fullfile(FileDir, [FilePre 'red.fig']))
print(FigH, fullfile(FileDir, [FilePre 'red.png']), '-r300', '-dpng')
end
Hi Steven,
No, the filename does not end with the 'fig' extension. According to (https://uk.mathworks.com/help/matlab/ref/savefig.html), I don't need one as the instructions seem to suggest that using the format: savefig(H,filename) saves figures identified by the graphics array H as filename.fig. What am I missing? My filename works with the initial code I have written in my initial question just fine. I am now trying to save the figures as fig files instead of jpg.
BM
2018 年 10 月 16 日
Hi OCDER,
Your code seems to work, but only will save 1 out of the 3 figures produced each iteration. I modified it slightly to save all three figures! Works though!
BM
2018 年 10 月 16 日
Thanks again!
OCDER
2018 年 10 月 16 日
You're welcome!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Printing and Saving についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
