How to save a plot as a file, after each iteration off a for loop.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
2 votes
I was wondering if it's possible to save a plot after each iteration of a for loop, and adda Date, Latitude and Longtitude to the title?
Here is the script I'm working on(the variables are added from a 5 column cell array(newdata)):
D = newdata(:,2);%depth
S = newdata(:,1); %salinity
t1 = cell2mat(newdata(:,3)); %days since 1950-01-01
t2 = 19500101;
t3 = datetime(t2,'ConvertFrom','yyyymmdd');
t4 = juliandate(t3);
t5 = t1 + t4;
T = datetime(t5,'convertfrom','juliandate'); %datenumtime
Lat = newdata(:,4);
Long = newdata(:,5);
[L,W] = size(newdata);
for i = 1 : L
x = S{i,1};
y = D{i,1};
plot(x,y),hold on
scatter(x,y),hold off
legend
xlabel('Salinity')
ylabel('depth [m]')
end
Would appreciate som help.
Accepted Answer
Ameer Hamza
on 14 Apr 2020
Edited: Ameer Hamza
on 14 Apr 2020
See exportgraphics(): https://www.mathworks.com/help/matlab/ref/exportgraphics.html. You can call it inside the for loop to save the figures
for i = 1 : L
x = S{i,1};
y = D{i,1};
plot(x,y),hold on
scatter(x,y),hold off
legend
xlabel('Salinity')
ylabel('depth [m]')
title(sprintf('Date:%s Lat:%d Lon:%d', string(t5(i)), Lat(i), Long(i)));
exportgraphics(gca, ['file' num2str(i) '.png'])
end
15 Comments
johndoe
on 14 Apr 2020
Great thanks! :)
Ameer Hamza
on 14 Apr 2020
Glad to be of help.
johndoe
on 14 Apr 2020
When I use:
exportgraphics(gca, ['file' num2str(i)] '.png')
i get this error:
Error: File: Skript_haloklin_plots.m Line: 23 Column: 41
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
Do you know what could be the cause of that?
Ameer Hamza
on 14 Apr 2020
What is the value of variable 'i' when you run this line?
johndoe
on 14 Apr 2020
sorry for being such a noob, but how do I check that? :) It should be 1 right?
Ameer Hamza
on 14 Apr 2020
If you are inside a loop, you can use loop variable. If you are just using it alone, then you can write
exportgraphics(gca, 'file.png')
Either way, give a numeric value to i if you want to use that code.
johndoe
on 14 Apr 2020
Ok, thanks. I would like to use it to save an image of a plot for each iteration. So I guess i should try to get it to work inside loop then? It's one of those red marks at the side of line that says: "invalid syntax at '.png A'('might be missing a closing ')' " Could it be something with that?
Ameer Hamza
on 14 Apr 2020
Can you paste the exportgraphics line from your code. Make sure that it have same brackets as shown in my code.
johndoe
on 14 Apr 2020
exportgraphics(gca, ['file' num2str(i)] '.png')
here it is.
Ameer Hamza
on 14 Apr 2020
Correct the brackets
exportgraphics(gca, ['file' num2str(i) '.png'])
johndoe
on 14 Apr 2020
really appreciate you taking the time helping me out :)
now I got this error message instead though, do you know what could be wrong?
Unrecognized function or variable 'exportgraphics'.
Error in Skript_haloklin_plots (line 23)
exportgraphics(gca,['file' num2str(i) '.png'])
Ameer Hamza
on 14 Apr 2020
Edited: Ameer Hamza
on 14 Apr 2020
Which MATLAB are you using? If older than 2020a, the use the following line instead of exportgraphics
saveas(gcf, ['file' num2str(i) '.png'])
johndoe
on 15 Apr 2020
yeah, it was 2019. Works great now. Thanks so much! :)
Ameer Hamza
on 15 Apr 2020
Glad to be of help.
mehmet durmaz
on 17 Apr 2022
thanks a lot.
More Answers (0)
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)