unable to save a figure in for loop

조회 수: 9 (최근 30일)
Alan
Alan 2024년 12월 17일
편집: Cris LaPierre 2024년 12월 18일
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
Stephen23
Stephen23 2024년 12월 18일
Or using FULLFILE:
figname = fullfile("Figures", latname + "," + longname + ".svg");

댓글을 달려면 로그인하십시오.

추가 답변 (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