unable to save a figure in for loop

7 views (last 30 days)
Hello,
I've created a geoplot figure and I need to take some zoomed in screenshots of the figure. I've created the following code to do so:
for i =0:35
minlat=40.4+i*(1/60);
maxlat=40.4+(i+1)*(1/60);
latcent=(minlat+maxlat)/2;
latname=replace(num2str(latcent),".","_");
for j=0:26
minlong=-75.9+j*(2/60);
maxlong=-75.9+(j+1)*(2/60);
longcent=(maxlong+minlong)/2;
longname=replace(num2str(longcent),".","_");
geolimits([minlat,maxlat],[minlong,maxlong]);
%drawnow
%mainfigure=get(1);
figname=["Figures\",latname,",",longname,'.svg'];
%disp("asdf")
print(gcf,'-vector','-dsvg',figname);
%disp("asdfasdf")
end
end
When I run this code, I keep getting the following error with no .svg files created:
Error using checkArgsForHandleToPrint
Invalid graphics object.
Error in checkArgsForHandleToPrint
Error in print>LocalCreatePrintJob (line 108)
handles = checkArgsForHandleToPrint(0, varargin{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in print (line 38)
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in LV_Dev (line 798)
print('-f1','-vector','-dsvg',figname);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
which is really weird because if i just do the final print statement in the command prompt on its own, I get the screenshot I need. For whatever reason, it seems like matlab can't get or set a figure while in a for loop. I've commented out some of my other attempts to get this to work.
I really don't want to do this manually so any help would be appreciated.
Thanks
  2 Comments
Stephen23
Stephen23 on 17 Dec 2024
The line of code shown in the error message
print('-f1','-vector','-dsvg',figname);
is not present in the code you have quoted above.
Alan
Alan on 17 Dec 2024
oh ya sorry, that was another attempt at getting this to work. Regardless of what I put as the first argument in print, it comes up with this error

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 18 Dec 2024
Edited: Cris LaPierre on 18 Dec 2024
I think the issue is with how you are building figname.
i =0;
minlat=40.4+i*(1/60);
maxlat=40.4+(i+1)*(1/60);
latcent=(minlat+maxlat)/2;
latname=replace(num2str(latcent),".","_");
j=0;
minlong=-75.9+j*(2/60);
maxlong=-75.9+(j+1)*(2/60);
longcent=(maxlong+minlong)/2;
longname=replace(num2str(longcent),".","_");
figname=["Figures\",latname,",",longname,'.svg']
figname = 1x5 string array
"Figures\" "40_4083" "," "-75_8833" ".svg"
MATLAB is interpretting that as 5 separate items, which get treated as 5 separate inputs to your function. I would construct figname using the following
figname = "Figures\" + latname + "," + longname + ".svg"
figname = "Figures\40_4083,-75_8833.svg"
However, if saveas is already working, then I don't see a reason to make it work using print.
  1 Comment
Stephen23
Stephen23 on 18 Dec 2024
Or using FULLFILE:
figname = fullfile("Figures", latname + "," + longname + ".svg");

Sign in to comment.

More Answers (1)

Alan
Alan on 18 Dec 2024
For whatever reason, matlab didnt like using the print function for this application, but this worked perfectly fine when i changed the print to saveas:
saveas(gcf,figname,'svg')
I assume this has something to do with how print works in the command window vs in actual code, but I'm not sure and this was sure confusing

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!