unable to save a figure in for loop

9 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2024-12-17
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 2024-12-17
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

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-12-18
编辑:Cris LaPierre 2024-12-18
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 个)

Alan
Alan 2024-12-18
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

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by