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 Exchange에서 Develop 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