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

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

采纳的回答

Adam
Adam 2019-1-16
编辑:Adam 2019-1-16
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 );

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by