save several plots in seperate files within a live script
24 ビュー (過去 30 日間)
表示 古いコメント
hello there,
i have a live script which plots the results at its end. it creates a new figure and title and so on. now i want to save all the figures. the figures are being created in a function, cause there is a lot of different data, but more or less the same plot.
i have found 2 solutions so far, whcih sadly dont work.
the first solution i used had the problem that it doesnt save it according to the names. they just have no name and there are overwriting each other. another problem is, that out ofnowhere it gave me the error that the folder is incorrect. i made another folder for the plots and the folder does exist, matlab knows the folder i added it.
the 2nd solution gave me the following error message
3 件のコメント
採用された回答
Nathan Hardenberg
2023 年 5 月 24 日
編集済み: Nathan Hardenberg
2023 年 5 月 24 日
"the 2nd solution" works fine for me. You have probably edited the code wich resulted in an error. Note that you can copy and paste the itire solution (try that in a new .m or .mlx file) and do not have to change anything to get it to work.
path = pwd ; % mention your path
myfolder = 'myfolder' ; % new folder name
folder = mkdir([path, filesep, myfolder]) ;
path = [path, filesep, myfolder];
for k = 1:10
figure(k);
plot(rand(1,10));
temp=[path, filesep, 'fig', num2str(k), '.png'];
saveas(gca,temp);
end
Your error-message seems to indicate that you have put a wrong path into the saveas()-function. Check the documentation for it for further information.
If you path is '.\.png' like the error-message suggests, your path is missing a filename. A valid path for example would be:
".\nameOfOutputFile.png"
But you have to change the filename during the loop. Otherwise your file will be overwritten (this is also done in the solution above).
3 件のコメント
Walter Roberson
2023 年 5 月 24 日
Recommended style changes:
figure(1);
plot(rand(1,10));
figure(2);
plot(rand(1,10));
figure(3);
plot(rand(1,10));
mypath = pwd ; % mention your path
myfolder = 'myfolder' ; % new folder name
folder = fullfile(mypath, myfolder);
if ~isdir(fullfolder); mkdir(folder); end
for k = 1:3 % <--- Enter right amount of figures (3 in this case)
figure(k); % select figure
temp = fullfile(path, "fig" + k + ".png");
saveas(gca, temp);
end
その他の回答 (1 件)
Andre
2023 年 5 月 24 日
1 件のコメント
Nathan Hardenberg
2023 年 5 月 24 日
編集済み: Nathan Hardenberg
2023 年 5 月 24 日
Don't confuse Name and Title. The name is given to the figure when it is initialized (see following Code). The Title is the "Title" of the Axis-object, which is a child of the figure-object. If you would initialize all figures like seen below, the code from the solution1 should work. But by default no name is given to a figure.
fig1 = figure("Name", "myName");
fig1.Name
fig2 = figure;
fig2.Name
And nice to see that you found a way it works for you
参考
カテゴリ
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!