MATLAB Answers

How to click stop a function in while loop using a pushbotton in GUI?

3 views (last 30 days)
George
George on 30 Jun 2015
Commented: kausalya on 2 Jul 2015
I have two buttons. When DC_start is pressed it disables itself and two user inputs, and then enters a while loop. At some point the user will click the DC_end button and it changes its value to 1 and the while loop is supposed to break.
A simplified version of the code is provided below with the error. Please let me know how to overcome this as its my first time making a GUI. I believe I have to use the uiwait function and suppress the warning, but even then I am confused at how to use the handle for the DC_end button if its not in the scope of the DC_start callback function.
If there is a completely different but better way to code this up. I'm open to suggestions.
Thanks in advance, George
function DC_start_pushbutton1_Callback(hObject, eventdata, handles)
set(handles.DC_start_pushbutton1,'Enable', 'off')
set(handles.DC_numSamples_edit, 'Enable', 'off')
set(handles.DC_electrodesList_edit, 'Enable', 'off')
channelNum = 0;
dataEntry = 0;
endButton = get(handles.DC_end_pushbutton2,'Value');
while( ~isequal(endButton, 1))
endButton = get(handles.DC_end_pushbutton2,'Value');
channelNum = channelNum + 1; dataEntry = dataEntry + 1;
pause(0.500);
if endButton == 1;
disp('Data collection canceled!')
break;
end
end
disp('Done!');
function DC_end_pushbutton2_Callback(hObject, eventdata, handles)
set(handles.DC_end_pushbutton2,'Value',1)
Error Message:
Undefined function or variable 'ElectrodeGUI'.
Error in
@(hObject,eventdata)ElectrodeGUI('DC_end_pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error using pause
Error while evaluating UIControl Callback

  0 Comments

Sign in to comment.

Accepted Answer

kausalya
kausalya on 30 Jun 2015
Hi George,
You could use uiwait instead of pause which would eliminate one error. To answer your second question, to access handles or data of a control when you are out of scope of that control, you can use findobj.
h = findobj('Tag','DC_end_pushbutton2'); %variable h has the handles of pushbutton2 and can be accessed anywhere. data = h.UserData; % For R2014a and earlier: % data = get(h,'UserData');
hope this helps.
Thanks

  2 Comments

George
George on 30 Jun 2015
Thanks a lot for the findobj help. If my pauses need to be under a second is there a workaround for the whole warning that uiwait throws up if your wait time is less than a second?
kausalya
kausalya on 2 Jul 2015
if you do not want to use uiwait then you could just use pause(1) which would pause for a sec.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by