How to click stop a function in while loop using a pushbotton in GUI?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
kausalya
2015-6-30
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 个评论
kausalya
2015-7-2
if you do not want to use uiwait then you could just use pause(1) which would pause for a sec.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!