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)

 Respuesta aceptada

Jon
Jon el 1 de Mayo de 2023

0 votos

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

7 comentarios

Degaulle
Degaulle el 1 de Mayo de 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 el 1 de Mayo de 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 el 1 de Mayo de 2023
The thing Checkbox2 should act on Spinner2, CheckBox3 on Spinner3.
Not all of them on Spinner
Jon
Jon el 1 de Mayo de 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 el 1 de Mayo de 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 el 1 de Mayo de 2023
Thanks!
Jon
Jon el 1 de Mayo de 2023
Your welcome - that was interesting

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Etiquetas

Preguntada:

el 1 de Mayo de 2023

Comentada:

Jon
el 1 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by