I am new to Matlab so please bear with me.
So I created a two GUID GUI in which one generates a dynamic data and updates the plotting every second so I used pause(1) (which is continuous) and the second one takes the full data of the first GUI and plots it (opens on button press)
Is there a way where if I open the the second GUI the first GUI pauses and if and only if the second GUI is stopped the first GUI resumes its process?
Thanks in advance.
Update
Example:
gui1.m
function guie1()
for ii=1:100
c = magic(ii)
plot(c);
% a button at this point
% Some pause condition
drawnow;
end
so when I click on that button it would open a window (a new figure may be) so unless I close is the loop should be paused.
Here is the example:
run(fullfile(docroot,'techdoc','creating_guis','examples','callback_interrupt'));
Here is the link:
http://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html
Update:
Here:
http://blogs.mathworks.com/videos/2010/12/03/how-to-loop-until-a-button-is-pushed-in-matlab/
http://www.mathworks.com/matlabcentral/fileexchange/29618-spspj
Related
I'm using a programmatic GUI in MATLAB which uses multiple figure windows. When I press the button 'Redraw' in Figure A, a new figure appears (Figure B) with some data plotted. I want the focus to immediately switch back to Figure A because there are many hotkeys (WindowKeyPressFcn) that I use in that window to update the plots in Figure B.
There are two problems here:
1) The last line of the callback for the button 'Redraw' does switch focus back to Figure A, BUT only if Figure B exists already. That is, the first time Figure B is created, it remains in focus. If I then use Figure A to update the plots in Figure B, the focus correctly switches back to Figure A. I can't think of why it behaves differently during the first redraw and all subsequent calls.
2) The even bigger issue is that if I set a breakpoint anywhere in the code and then resume execution, the focus switches back to Figure A as I want. So, why does entering the debugger and doing nothing else fix the problem? How can I find the issue if everything works in the debugger?
Thanks in advance!
EDIT: To my great surprise, I was able to reproduce this "Heisenbug" by writing my first ever programmatic GUI. This should be the simplest example of my problem. To see it in action, simply run the code below and click on the push button. For some reason, when Window 2 is created for the first time, the focus does NOT switch back to Window 1 as intended. It works properly for all subsequent button presses. Try closing Window 2 and pushing the button again, the error will keep occurring.
As mentioned in the original post, setting a breakpoint in the code resolves the issue. Set a breakpoint at line 27, then resume execution and Window 1 will be in focus.
What is happening here?
function heisenbug
%% Main problem:
% After clicking the push button, I want the focus to
% always switch back to Window 1 (the one containing the button).
% However, this does not work when Window 2 is first created.
%%
%% Create and then hide the GUI as it is being constructed
f = figure('Visible','off','name','Window 1','units','normalized','Position',[0.1 0.1 0.5 0.5]);
%% Initialize handles structure
handles = guihandles(f);
handles.window2 = [];
guidata(f,handles)
%% Make a button
hbutton = uicontrol('Style','pushbutton','String','Push me','units','normalized',...
'Position',[0.1 0.1 0.8 0.8],...
'Callback',#button_Callback);
%% Make the GUI visible
f.Visible = 'on';
%% Button callback
function button_Callback(source,eventData)
handles = guidata(gcbo);
% If Window 2 already exists, plot a circle, then switch focus back to Window 1.
if ~isempty(handles.window2) && ishandle(handles.window2)
figure(handles.window2);
plot(1,1,'bo')
figure(f);
% Otherwise, create Window 2 and do the same thing.
else
handles.window2 = figure('Name','Window 2','units','normalized','position',[0.4 0.1 0.5 0.5]);
plot(1,1,'bo')
figure(f)
end
guidata(source,handles)
end
end
My post was quickly answered by Adam (thanks!) on the MathWorks site: http://se.mathworks.com/matlabcentral/answers/299607-simply-entering-the-debugger-during-execution-of-matlab-gui-fixes-error-that-persists-during-normal.
I needed to insert a pause(0.05) after Window 2 is created, before trying to switch the focus back with figure(f). Otherwise the focus is stolen back to Window 2 when it finishes plotting.
I have a question that I can seem to wrap my head around,
I have created a gui that uses the unput from the user to run a script within the gui and later display the results to the user interface. What I dont understand is that everything is running ok until I choose one particular file. The interesting and mind boggling is that the script jumps a nested loop (whie this dosent happen for the other choices). I dont understand why this happens and cant find anything relavent on the internet. The script stand alone executes perfectly.
for i=2:length(Data_last_values)
for j=2:length(temp_nov)
if Data_last_values(i,4)>temp_nov(j,1) & Data_last_values(i,4)<temp_nov(j-1,1)
Data_last_values(i,9)=Data_last_values(i,3)+Q_leak_dot(j);
disp('gone through loop')
end
end
end
displayed above is the part of the nested loop that matlab chooses to skip. would any one know why this happens? below I present the gui script:
if not(isempty(cell2mat(z)))
run Daughter
assignin('base','avg',data(:,8))
axes(handles.axes1);
markerSize = 50;
scatter(Data_last_values(:,4),Data_last_values(:,9),markerSize,Data_last_values(:,5)) % -> volumetric mass flow is used in the color bar
colormap(jet(20));
h=colorbar;
xlabel('T_{water}[C]')
ylabel('Q_{out} [W]')
ylabel(h, 'Volumetric Flow [Lt/min]')
grid minor
myvar = evalin('base', 'avg');
y=line([0 max(Data_last_values(:,4))], [mean(myvar) mean(myvar)]);
xlim([0 max(Data_last_values(:,4))])
In one GUI (viewer) I have an image that shows a 2D slice through a 3D image cube. A toolbar button opens a second GUI (z-profile) that plots a 2D graph showing the z-profile of one pixel in the image cube. What I want is to be able to update this plot dynamically when a different pixel is clicked in the original viewer GUI. I've looked in to linkdata but I'm not sure if that can be used to link across two GUIs. Is there a simple way to do this without re-creating the second GUI each time a new pixel is clicked and feeding in the new input location?
You can definitely doing it without recreating the second GUI every time.
Without knowing your specific code I would say that you should store a reference to the second GUI in the first GUI, then in a callback for clicking a pixel in the first GUI, change data in the second GUI via the stored reference (e.g. figure handle). You can store arbitrary data in a figure, for example by using function guidata. A bit of code.
...
figure2 = figure();
figure1 = figure('WindowButtonDownFcn',#myCallback);
guidata(figure1, figure2);
...
function myCallback(obj,eventdata)
figure2 = guidata(obj);
...
Even easier but a bit more error-prone would be to use global variables for storing the references.
I am trying to implement a very simple GUI that consists of just one pushButton. I want it to begin by just having START as a label. Then on press it changes to STOP. When the user clicks the button the first time the callback sets a boolean to true and changes the label. When the Button is clicked a second time the boolean is changed to false and the GUI closes.
I can't find anything on how to make a simple GUI like this in MATLAB. the GUIDE tool makes no sense to me and seems to generate so much useless code. Matlab buttons are wrappers for jButtons as seen here
GUIDE is quite straightforward - the automated tool generates stubs for all the callbacks, so that all is left is to fill in the code to be executed whenever the callback runs. If you prefer to create the GUI programmatically, you can create the button you want as follows:
%# create GUI figure - could set plenty of options here, of course
guiFig = figure;
%# create callback that stores the state in UserData, and picks from
%# one of two choices
choices = {'start','stop'};
cbFunc = #(hObject,eventdata)set(hObject,'UserData',~get(hObject,'UserData'),...
'string',choices{1+get(hObject,'UserData')});
%# create the button
uicontrol('parent',guiFig,'style','pushbutton',...
'string','start','callback',cbFunc,'UserData',true,...
'units','normalized','position',[0.4 0.4 0.2 0.2])
I'm working on a gui using GUIDE in MATLAB, and from what I've read it looks like MATLAB updates the UI controls based on a timer every so often. Is there a way to force it to update the UI controls, so I can make it update in the middle of the function? Right now I have a function that does, simplifed, something like
set(handles.lblStatus,'String','Processing...')
%function that takes a long time
set(handles.lblStatus,'String','Done')
Since MATLAB doesn't update the GUI during a Callback function, the user only ever sees 'Done' after a long period of waiting and never sees 'Processing'. I tried adding guidata(hObject, handles) after the first set, hoping it would force the screen to update, but it doesn't.
Try calling DRAWNOW.
set(handles.lblStatus,'String','Processing...')
drawnow
%function that takes a long time
set(handles.lblStatus,'String','Done')
I believe there is a drawnow function in matlab.
drawnow completes pending drawing events