How to use toogle button to switch on and off a push button?

18 views (last 30 days)
when i push the toggle button, it displays on and off but I want that when I pressed the toggle switch to on, the "add" pushbutton will execute. I'm thinking of adding a while loop to the code of "add" button but i dont know how. I want my add button executes only when the toggle button is on.
here's my code:
% --- Executes on button press in add.
function add_Callback(hObject, eventdata, handles)
% hObject handle to add (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA);
persistent add
count=handles.count;
if isempty(add)
add=count;
add=add+1;
else
add=add+1;
end
handles.add=add;
set(handles.screen,'String',add);
guidata(hObject, handles);
% --- Executes on button press in on_offbutton.
function on_offbutton_Callback(hObject, eventdata, handles)
% hObject handle to on_offbutton (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 on_offbutton
button_state = get(hObject,'Value');
if(button_state == 1)
set(handles.on_offbutton,'String','ON','ForegroundColor','green')
else
set(handles.on_offbutton,'string','OFF','foregroundcolor','red')
end

Accepted Answer

Cris LaPierre
Cris LaPierre on 25 Jan 2021
If I understand correctly, you have a toggle button and a pushbutton. When you set the toggle button to 'on', you want the pushbutton callback to execute. Is that correct?
If that is correct, the trick is perhaps to realize that your callbacks are just functions. You can call them just like any function. The solution here is to call the pushbutton callback from within the toggle callback function (when value is 'on'). Just be sure to set the first input to be the corresponding component object instead of hObject.
% --- Executes on button press in on_offbutton.
function on_offbutton_Callback(hObject, eventdata, handles)
% hObject handle to on_offbutton (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 on_offbutton
button_state = get(hObject,'Value');
if(button_state == 1)
set(handles.on_offbutton,'String','ON','ForegroundColor','green')
% Call the add callback function.
add_Callback(handles.add_callback, eventdata, handles)
else
set(handles.on_offbutton,'string','OFF','foregroundcolor','red')
end
  4 Comments
Sofia Ventura
Sofia Ventura on 27 Jan 2021
When I switch the toggle button to "off" i want it to stop adding when i press the add button.
Cris LaPierre
Cris LaPierre on 27 Jan 2021
Edited: Cris LaPierre on 27 Jan 2021
When you push the Add button, it will call the add_Callback function.
There are a couple options for how I might approach this
  1. Use the toggle button to set the 'Enable' property of the Add button 'on' and 'off' or
  2. Add a condition to your add callback to only execute if the toggle switch is on.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!