I'm building an app using app designer and I have these chunks of code:
function CheckBoxValueChanged(app, event)
value = app.CheckBox.Value;
if get(app.CheckBox,'value')
set(app.Spinner, 'Visible','on')
set(app.Spinner, 'Enable','on')
end
end
function CheckBox2ValueChanged(app, event)
value = app.CheckBox2.Value;
if get(app.CheckBox2,'value')
set(app.Spinner2, 'Visible','on')
set(app.Spinner2, 'Enable','on')
end
end
function CheckBox3ValueChanged(app, event)
value = app.CheckBox.Value;
if get(app.CheckBox3,'value')
set(app.Spinner3, 'Visible','on')
set(app.Spinner3, 'Enable','on')
end
end
I want to know how can i simplify the code(for example: using arrays or something similar)

 Accepted Answer

Jon
Jon on 1 May 2023

0 votes

Define one function to do the work, and then call this same function from each of the callbacks

7 Comments

The thing is each callback acts on a different spinner, how can i make a function that works with 2 different properties
So for example
methods (Access = private)
function changeSpinner(app,checkboxValue)
if checkboxValue
set(app.Spinner, 'Visible','on')
set(app.Spinner, 'Enable','on')
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: CheckBox1
function CheckBox1ValueChanged(app, event)
value = app.CheckBox1.Value;
changeSpinner(app,value)
end
% Value changed function: CheckBox2
function CheckBox2ValueChanged(app, event)
value = app.CheckBox2.Value;
changeSpinner(app,value)
end
% Value changed function: CheckBox3
function CheckBox3ValueChanged(app, event)
value = app.CheckBox3.Value;
changeSpinner(app,value)
end
end
The thing Checkbox2 should act on Spinner2, CheckBox3 on Spinner3.
Not all of them on Spinner
I think this now does what you are asking.
methods (Access = private)
function changeSpinner(~,checkboxValue,spinner)
if checkboxValue
set(spinner, 'Visible','on')
set(spinner, 'Enable','on')
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: CheckBox1
function CheckBox1ValueChanged(app, event)
value = app.CheckBox1.Value;
changeSpinner(app,value,app.Spinner1)
end
% Value changed function: CheckBox2
function CheckBox2ValueChanged(app, event)
value = app.CheckBox2.Value;
changeSpinner(app,value,app.Spinner2)
end
% Value changed function: CheckBox3
function CheckBox3ValueChanged(app, event)
value = app.CheckBox3.Value;
changeSpinner(app,value,app.Spinner3)
end
end
Here is a much cleaner way to do it. Just use one callbacack function (shown below) and assign this to all of the checkboxes. The callback function itself figures out who called it and which spinner it should change.
You must use a consistent naming convention for the spinners and checkboxes for this to work, e.g. CheckBox# and Spinner#
% Value changed function: CheckBox1, CheckBox2, CheckBox3
function CheckBoxValueChanged(app, event)
% determine source checkbox
checkbox = event.Source;
checkboxNo = checkbox.Text(end); % '1', '2',...
% build the name of the associated spinner
spinnerName = "Spinner" + checkboxNo;
% enbable the spinner if checkbox is checked
set(app.(spinnerName), 'Visible',checkbox.Value)
set(app.(spinnerName), 'Enable',checkbox.Value)
end
Thanks!
Jon
Jon on 1 May 2023
Your welcome - that was interesting

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 1 May 2023

Commented:

Jon
on 1 May 2023

Community Treasure Hunt

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

Start Hunting!