How to click stop a function in while loop using a pushbotton in GUI?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
kausalya
le 30 Juin 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 commentaires
kausalya
le 2 Juil 2015
if you do not want to use uiwait then you could just use pause(1) which would pause for a sec.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!