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)

 Réponse acceptée

Jon
Jon le 1 Mai 2023

0 votes

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

7 commentaires

Degaulle
Degaulle le 1 Mai 2023
The thing is each callback acts on a different spinner, how can i make a function that works with 2 different properties
Jon
Jon le 1 Mai 2023
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
Degaulle
Degaulle le 1 Mai 2023
The thing Checkbox2 should act on Spinner2, CheckBox3 on Spinner3.
Not all of them on Spinner
Jon
Jon le 1 Mai 2023
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
Jon
Jon le 1 Mai 2023
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
Degaulle
Degaulle le 1 Mai 2023
Thanks!
Jon
Jon le 1 Mai 2023
Your welcome - that was interesting

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by