how to set togglebutton value from another togglebutton

3 Ansichten (letzte 30 Tage)
alex
alex am 6 Mär. 2016
Beantwortet: Helmut Riedel am 22 Mär. 2017
Hello,
i have two togglebuttons in my gui which i created from guide.
i want when i press the first one then automatically pressed and the second one.
in the first toggle button i set:
set(handles.togglebutton2, 'Value', 1);
The togglebutton2 seems to be pressed,however the code in the togglebutton2 doesn't execute.
Any idea what to do to execute the code in the togglebutton2 when i press the first one?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Mär. 2016
You cannot get the second toggle to run just by setting its Value. You need to call its callback. If you constructed your GUI using GUIDE, that might look something like
evalin('base', get(handles.togglebutton2, 'Callback'))
but you could probably instead call
togglebutton2_Callback(handles.togglebutton2, event, handles)
where "event" should be named the same thing as the second parameter to the callback you are executing at the time.
  3 Kommentare
Walter Roberson
Walter Roberson am 6 Mär. 2016
I have tested and confirmed that any remaining code in the togglebutton1_Callback will be executed after calling togglebutton2_Callback .
If you are setting variables in togglebutton2_Callback and expecting the variables to be available in togglebutton1_Callback, then remember that the two functions have distinct workspaces and that variables set in one will not appear in the other.
If you are setting something in the handles structure in togglebutton2_Callback and expecting the changed value to be available in togglebutton1_Callback, then you need to have used guidata() to update the "master" copy of the handles structure, and then after you call to togglebutton2_Callback, you need to call guidata() in togglebutton1_Callback to fetch the updated copy of "handles".
alex
alex am 7 Mär. 2016
this is the code of the two functions. if i run it as it is the result is that i see results in the edit2, but nothing in the edit 1.
function togglebutton1_Callback(hObject, eventdata, handles)
ispushed=get(hObject,'Value');
if ispushed
set(hObject, 'String', 'STOP');
else
set(hObject, 'String', 'START');
end
set(handles.togglebutton2, 'Value', 1);
togglebutton2_Callback(handles.togglebutton2, eventdata, handles)
x=0;
set(handles.edit1, 'String', x);
function togglebutton2_Callback(hObject, eventdata, handles)
tic;
while get(hObject,'Value')
b=toc;
set(handles.edit2, 'String', floor(b));
drawnow
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Helmut Riedel
Helmut Riedel am 22 Mär. 2017
Hi Alex,
I implemented a function togglegroup.m that can toggle 5 togglebuttons with a master togglebutton. Interestingly, the actual callback function has not to do anything, since the button states (and their changes/toggles) are read and set in the main function.
Hope that helps.
Cheers,
Helmut

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by