Reputation: 1649
I have a Matlab gui created in GUIDE. When I click a button in gui_A
, it opens a new figure window for gui_B
(different fig and m files for both). The plot takes a long time to generate each item on it, so I want to plot each item as soon as it is ready. However, I can't seem to figure out how to get the window to render FIRST, and THEN plot things on it. I've tried using pause(1)
and drawnow
but neither have the desired effect. Here's some sample code for what I'm doing:
in gui_A
function open_gui_b_btn(hObject, eventdata, handles)
gui_B(handles.var1, handles.var2);
in gui_B
function gui_B_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.var1 = varargin{1};
handles.var2 = varargin{2};
% set some variables here
% plot something on an axis
% Try to render on screen
% drawnow % doesn't work
% pause(1) % doesn't work
% plot the rest of the things
plot_things(handles)
function plot_things(handles)
for i = 1:length(handles.something)
% computationally expensive process
plot(handles.axis1, handles.var1b, handles.var2b);
end
Obviously I would like for the window of gui_B
to render on the screen and then have the plot things function plot all the things as soon as they are ready so the user can watch the progress.
Upvotes: 1
Views: 108
Reputation: 65430
The issue is that when loading the GUI, GUIDE will (by default) set the Visible
property of the figure to 'off'
so that a potentially incomplete GUI won't be shown to the user. It will toggle the Visible
property back to 'on'
after all initialization is complete. You can alter this behavior by manually settings the Visible
property to 'on'
within your OpeningFcn
function gui_B_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.var1 = varargin{1};
handles.var2 = varargin{2};
% Make sure the figure is visible
set(hObject, 'Visible', 'on')
drawnow
Or you can initialize your GUI by providing the Visible
property in the function call
gui_B('Visible', 'on')
After doing that, you'll also want to add explicit drawnow
commands after each plot command to cause the graphics to be rendered right away.
function plot_things(handles)
for i = 1:length(handles.something)
% computationally expensive process
plot(handles.axis1, handles.var1b, handles.var2b);
% Draw each plot as it's available
drawnow
end
Upvotes: 0