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

조회 수: 6 (최근 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 );
  댓글 수: 1
Ruger28
Ruger28 2019년 1월 16일
Adam, Thank you very much for the quick answer! It does exactly as needed. I greatly appreciate the help nd learning experience.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by