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

6 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Adam
Adam le 16 Jan 2019
Modifié(e) : Adam le 16 Jan 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 commentaire
Ruger28
Ruger28 le 16 Jan 2019
Adam, Thank you very much for the quick answer! It does exactly as needed. I greatly appreciate the help nd learning experience.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by