unable to save a figure in for loop
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alan
el 17 de Dic. de 2024
Editada: Cris LaPierre
el 18 de Dic. de 2024
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 comentarios
Stephen23
el 17 de Dic. de 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.
Respuesta aceptada
Cris LaPierre
el 18 de Dic. de 2024
Editada: Cris LaPierre
el 18 de Dic. de 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']
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 comentario
Stephen23
el 18 de Dic. de 2024
Or using FULLFILE:
figname = fullfile("Figures", latname + "," + longname + ".svg");
Más respuestas (1)
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!