openfig visibility when saved figure has a callback
77 ビュー (過去 30 日間)
古いコメントを表示
I used the method described here to save figures with a callback that would change their visibility to 'on' when opened.
fig = figure('Visible','off');
set(fig, 'CreateFcn', 'set(gcbo,''Visible'',''on'')');
plot([1:10]);
savefig(fig,"C:\temp\test.fig")
close all
openfig("C:\temp\test.fig",'invisible')
Behavior of sample code: figure opens and is visible
Desired Behavior: figure opens and is invisible.
Is this possible? Thank you.
5 件のコメント
dpb
2025 年 8 月 19 日 14:46
You still haven't answered the questions posed -- show the exact code you are using for the callback function.
If you just copied the example referenced, it specifically was for the reverse situation where a figure was saved as 'invisible' and the user wanted it to be automagically made visible on loading.
You're after the opposite, so if you used the same callback function, that's where the 'on' is coming from--but, you haven't shown us the actual code, just tried to describe it and symptoms.
The best would be to post a minimum working example that creates the problem by extracting the pertinent code from the app....but, specifically, the callback function code itself is needed...and, wouldn't hurt to attach an example image file just to be certain folks have the same set of conditions with which to experiment if so inclined...
採用された回答
dpb
2025 年 8 月 19 日 15:48
編集済み: dpb
2025 年 8 月 19 日 18:45
fig = figure('Visible','off');
set(fig, 'CreateFcn', 'set(gcbo,''Visible'',''on'')');
plot([1:10]);
savefig(fig,"C:\temp\test.fig")
close all
openfig("C:\temp\test.fig",'invisible')
That's exactly the issue Walter and I were after clarification of -- the callback function turns 'Visible' to the 'on' state as it is instructed to do.
It isn't possible to have your cake and eat it, too, no.
You need to either not save the callback with 'on' state specified (or not have the callback at all) or you'll have to then explicitly make the figure invisible after it is loaded and the callback function is done.
I guess the other alternative could be to have the callback be an m-file function that contains a logical branch as to whether to be visible or not based on some discernible other condition such as is in compiled app or not -- or whatever it is that is the deal here as to why you're telling it to do two different mutually exclusive things.
Alternatively, the cell array form for the callback function allows to pass an argument list that could be the desired state -- although I am not sure at the moment how one would set that value...would have to explore what actually is passed in the callback -- if that argument to openfig were passed, then you would have it and be able to modify the action.
1 件のコメント
dpb
2025 年 8 月 19 日 16:18
編集済み: dpb
2025 年 8 月 19 日 19:46
ADDENDUM
Creating a callback m-file and setting breakpoint shows that the arguments passed by openfig are the handle to the figure and the event field is an empty double so there is no way to know the argument 'invisible' was passed in the openfig call...there would, as surmised above, have to be some other way to determine which is supposed to win here.
その他の回答 (1 件)
Ritika Thusoo
2025 年 8 月 18 日 11:08
Hi,
You can use the ‘openfig(___,visibility)’ to specify whether to open the figure in a visible or invisible state. To display the figure, set visibility to 'visible'. If you do not want to display the figure, use the 'invisible' setting.
Sample code:
fig = openfig('MySavedPlot.fig', 'invisible');
% Create a figure and set it to be invisible
f = figure('Visible', 'off');
plot(sin(linspace(0, 2*pi, 100))); % Example plot
savefig(f, 'MySavedPlot.fig'); % Save the figure
close(f); % Close the figure
% Open the saved figure with visibility set to 'off'
fig = openfig('MySavedPlot.fig', 'invisible');
fig.Visible = 'off'; % Make the figure visible
% Make the fig.Visible = ‘on’ in case you wish to make the figure visible.
You can read about this further from the following ‘openfig’ documentation page: https://www.mathworks.com/help/releases/R2025a/matlab/ref/openfig.html
参考
カテゴリ
Help Center および File Exchange で Programming Utilities についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!