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
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.
采纳的回答
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']
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"
However, if saveas is already working, then I don't see a reason to make it work using print.
1 个评论
Stephen23
2024-12-18
Or using FULLFILE:
figname = fullfile("Figures", latname + "," + longname + ".svg");
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!