how to set togglebutton value from another togglebutton

4 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2016-3-6
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 个评论
Walter Roberson
Walter Roberson 2016-3-6
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 2016-3-7
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

请先登录,再进行评论。

更多回答(1 个)

Helmut Riedel
Helmut Riedel 2017-3-22
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

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by