How to get value of radiobutton (or togglebutton) in GUI from another function?
Ältere Kommentare anzeigen
I have a GUI file with only a togglebutton and a pushbutton. The following is the code of .m file of the GUI (without code of initialization):
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton12
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=load('data.mat');
s=1;
g=[];
while s<=length(a)
g(s)=s*a(s);
if get(handles.radiobutton1,'Value')==0
break
end
s=s+1;
end
Basically, I want to get the value of the radiobutton and while it is not selected (value is 0) keep concatenating s*a(s) to array g. Then, when radiobutton is selected (its value is 1) stop the concatenation (finish), even if s is still <=length(a). However, when I press the push button I get the following error "reference to non-existent field handles.radiobutton1". How could I solve it?
PS: instead of radiobutton I could use togglebutton if required.
Thank you for your help.
3 Kommentare
Rik
am 22 Jun. 2018
There is probably a convoluted way to fix this (e.g. by searching through the children of the gcbf), but the easiest way to fix this, is making sure that the field is actually set somewhere in your init code. (don't forget to check that the handles struct is saved to guidata after that assignment)
Rik
am 22 Jun. 2018
Despite your edit, the relevant code is still missing. The handle to the radiobutton object should be saved to the struct somewhere. That doesn't change if you replace it by a togglebutton. Evidently the radiobutton object handle is not saved to the guidata struct under the name you expect. You could use breakpoints to see what fields handles contains. Otherwise, getting al the figure children is still an option.
Carlos HM
am 23 Jun. 2018
Antworten (0)
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!