Figure in nested loop when turned invisible gives error while trying to use saveas
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
T Leenderts
am 15 Sep. 2023
Kommentiert: T Leenderts
am 26 Sep. 2023
Due to a large dataset I am using a nested loop to create multiple figures.To prevent it spamming my livescript output side of the screen and decrease run time I am trying to turn the figures invisible. However, when I turn the figure invisible it gives an error at the saveas line. I am currently using fullfig() because it ensure the size of the saved figure but figure() gives the same error so it is used in the code below. In an earlier part of the script I use invisible figures to go from linear models to line objects of which I do not save the figure (only data points) and that works without issues. Due to this I am quite certain adding the invisiblity changes something in how I should save the file but I cannot find how or why and how to fix it. My preffered end result would be for the figures to get exported at max screen size, with the specified name and as png.
The nested loop looks like:
for oor = 2:8
for persoon = 2:5
f100 = figure(); %Ensure the figure is empty at beginning of loop
figure(f100); %Open said figure
subplot(1,4,1) %Open first subplot
hold on
%Code for double y-axis errorbar()
hold off
%And so on until subplot(1,4,4)
%Save figure with name specific to loop based upon array with
%strings
saveas(f100, Pnames(persoon) + "_" + Enames(oor) + ".png")
end
end
So changing
f100= figure();
to
f100 = figure('visible','off');
gives the error
Error using checkArgsForHandleToPrint
Handle input argument contains nonhandle values.
Error in checkArgsForHandleToPrint
handles = checkArgsForHandleToPrint(0, varargin{:});
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
print( h, name, ['-d' dev{i}] )
on line
saveas(f100, Pnames(persoon) + "_" + Enames(oor) + ".png")
where Pnames and Enames are arrays which include strings.
0 Kommentare
Akzeptierte Antwort
Yash
am 26 Sep. 2023
Bearbeitet: Yash
am 26 Sep. 2023
Hello,
I understand that you are facing issues with generating figures while attempting to disable their visibility. I tried running your code on my device using MATLAB R2022b, but I was unable to reproduce the error you have mentioned. I recommend you to first double-check the 'Pnames()' and 'Enames()' functions, as the error you encountered may be related to these functions.
However, I did encounter unexpected behaviour where the figures were visible in the MATLAB Live Editor output pane and separate windows were also created for the figures. Since this behaviour is not desirable in a live script, I understand the issue.
Upon reveiwing your code, I have found out that the below line is unnecessary and it is causing this unexpected behaviour:
figure(f100); % Open the specified figure
After removing this line, the figures were no longer displayed in the output pane or separate windows, and the saved figures were correct.
If this line serves an important puspose in you code and removing it is not an option, I have found a workaround to resolve this unexpected behaviour. You can use the 'set()' function to explicitly set the 'visible' property to 'off'.
Utilize the 'set()' function to set the 'visible' property as shown in the example below:
f100 = figure(); % Ensure the figure is empty at the beginning of the loop
figure(f100); % Open the specified figure
set(f100, 'visible', 'off'); % Make the figure invisible
subplot(1, 4, 1) % Open the first subplot
I hope this workaround helps you resolve the issue you are facing.
Best Regards,
Yash
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!