Saving plots during the run of a for-loop

1 回表示 (過去 30 日間)
Mahith Madhana Kumar
Mahith Madhana Kumar 2021 年 4 月 24 日
コメント済み: Image Analyst 2021 年 4 月 25 日
Hello,
I have couple of question on saving plots while a for-loop is continuously running. My code is:
interval=[0.01 0.1 0.5 1];
for i=1:length(x)
t=(0:interval(i):2*pi);
y = sin(t);
plot(t, y);
end
This code however shows the "Index exceeds the number of array elements (4)" error. I am not sure what is heppening here.
2) The second question is that I would like to save the output image file to a directory on my local folder as the fo-loop runs. The following code was used (for the same example above):
iinterval=[0.01 0.1 0.5 1];
for i=1:length(interval)
t=(0:interval(i):2*pi);
y = sin(t);
fig=figure(i);
plot(t, y);
basedir='C:\Users\Desktop\';
cd (basedir);
saveas(gca,fullfile(basedir,i),'.jpeg')
cd('C:\Users\');
end
The error shows: Unsupported format or extension: .jpg.
Basically I just need to see how the sine graphs vary with the size of the interval. Instead of manually doing things, I would love to have the plots saved to my destination as the for-loop runs but somehow I am running into more errors.
Thanks,
Mahith M
end

採用された回答

Mahith Madhana Kumar
Mahith Madhana Kumar 2021 年 4 月 24 日
編集済み: Mahith Madhana Kumar 2021 年 4 月 24 日
Hi,
I managed to edit the code slightly and this time no error occurs. However, its saving only the first file in my specified directory instead of expected 4. Here is the modified code:
interval=[0.01 0.1 0.5 1];
for i=1:length(interval)
t=(0:interval(i):2*pi);
y = sin(t);
fig=figure(i);
plot(t, y);
path='C:\Users\Desktop\images\';
saveas(fig,fullfile(path,['Sine' num2str(i) '.jpeg']));
end
  2 件のコメント
David Fletcher
David Fletcher 2021 年 4 月 24 日
編集済み: David Fletcher 2021 年 4 月 24 日
copied your code (the only change I made was to change the save path to something that exists on my system) and it ran fine on my system, saving all four of the graphs as Sine1.jpeg, Sine2.jpeg, Sine3.jpeg and Sine4.jpeg. No errors. Try creating/using a different folder
Image Analyst
Image Analyst 2021 年 4 月 25 日
path is a special keyword. Don't use it as the name for your folder or you may run into problems. Call it folder and assign it BEFORE your loop, not inside it.

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

その他の回答 (2 件)

David Fletcher
David Fletcher 2021 年 4 月 24 日
編集済み: David Fletcher 2021 年 4 月 24 日
1 - how big is x? If x is larger than interval you will get an indexing error.
2. Don't use the 'dot' in the saveas function (see below) Also may be just something you've done while entering the code into this forum, but you've spelt interval with two ii's at the start.
saveas(gca,fullfile(basedir,i),'jpg')
  1 件のコメント
Mahith Madhana Kumar
Mahith Madhana Kumar 2021 年 4 月 24 日
Hi,
Thank for the answer. I guess it does not have to do with the typos or the 'dot' in the saveas function. I am attaching the screenshot of what I am seeing after making the edits which you had aid. Notice that I am getting an output this time though but again with an error message.

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


Image Analyst
Image Analyst 2021 年 4 月 24 日
You can use exportgraphics() if you have R2020a or later. Otherwise see my attached demo where I plot and save, then make a movie from them all.
  3 件のコメント
Image Analyst
Image Analyst 2021 年 4 月 24 日
@Mahith Madhana Kumar, you can only accept one answer. Accept the one you think is best or helped you the most. The others you can "Vote" for, and you can Vote for as many answers as you want. Voting gives the answerer 2 reputation points just like Accepting does. Thanks.
Mahith Madhana Kumar
Mahith Madhana Kumar 2021 年 4 月 24 日
Allright thanks

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

カテゴリ

Help Center および File ExchangeScripts についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by