How to dynamically update a static text box depending on number of checkboxes selected.

1 visualización (últimos 30 días)
Hello,
I am currently trying to update a static text box in a MATLAB GUI I made. I am fairly new to GUIs, and I tried creating addlisteners, but to no avail.
I pre-set checkboxes in the opening function, and I want to be able to display the amount of boxes that are checked into a static text box . I have:
  • checkbox1 - checked
  • checkbox2 - not checked
  • checkbox3 - checked
  • checkbox4 - checked
  • checkbox5 - checked
I would like the static text box to display "4", and if I select the last checkbox, or deselect another checkbox, I would like the value to dynamically change.
% main loop
% --- Executes just before PnP_GUI is made visible.
function PnP_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to PnP_GUI (see VARARGIN)
% Choose default command line output for PnP_GUI
handles.output = hObject;
%Pre-Check Save Options
set(handles.checkbox1,'value',1)
%set(handles.checkbox2,'value',0) % want not checked to start
set(handles.checkbox3,'value',1)
set(handles.checkbox4,'value',1)
set(handles.checkbox5,'value',1)
% Update handles structure
guidata(hObject, handles);
Any help would be greatly appreciated.
Thank you,
- JR

Respuesta aceptada

Adam
Adam el 16 de En. de 2019
Editada: Adam el 16 de En. de 2019
One method:
Add this to your OpeningFcn before the guidata call
handles.hCheckboxes = [ handles.checkbox1, handles.checkbox2, handles.checkbox4, handles.checkbox4, handles.checkbox5 ]
Then create a function such as this in your GUIDE file and call it from each of your checkbox callbacks. Or you can point all checkboxes to a single callback if you wish, but they will each have their own by default.
function updateStaticText( handles )
totalSelected = sum( cell2mat( get( handles.hCheckboxes, 'Value' ) ) );
handles.text1.String = num2str( totalSelected );
  1 comentario
Ruger28
Ruger28 el 16 de En. de 2019
Adam, Thank you very much for the quick answer! It does exactly as needed. I greatly appreciate the help nd learning experience.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by