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)

 採用された回答

Jon
Jon 2023 年 5 月 1 日

0 投票

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

7 件のコメント

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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品

リリース

R2023a

質問済み:

2023 年 5 月 1 日

コメント済み:

Jon
2023 年 5 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by