Figure in nested loop when turned invisible gives error while trying to use saveas

1 次查看(过去 30 天)
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{:});
Error in print (line 38)
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
Error in saveas (line 181)
print( h, name, ['-d' dev{i}] )
on line
saveas(f100, Pnames(persoon) + "_" + Enames(oor) + ".png")
Unrecognized function or variable 'persoon'.
where Pnames and Enames are arrays which include strings.

采纳的回答

Yash
Yash 2023-9-26
编辑:Yash 2023-9-26
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 个评论
T Leenderts
T Leenderts 2023-9-26
Hello Yash,
It does indeed seem the
figure(f100);
Unrecognized function or variable 'f100'.
is not needed, thought it was somehow needed but do not even remember why...
Only removing that line did not solve the issue but adding the suggested line of
set(f100, 'visible', 'off'); % Make the figure invisible
while removing the open figure line did solve the issue.
You have my thanks Yash.
Best regards,
Thomas

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by