App Designer: How to store multiple Checkbox values as array in property variable?

19 次查看(过去 30 天)
Hello everybody,
I have an App with many Checkboxes, e.g.
app.1CheckBox app.2CheckBox app.3CheckBox
Now I would like to have an array storing the values of these checkboxes:
checkbox_states = [app.1CheckBox.Value,app.2CheckBox.Value,app.3CheckBox.Value]
Since I want to use checkbox_states for many different callback functions, I was wondering if I could add this variable as a public property? However this code does not seem to work:
properties (Access = public)
checkbox_states = [app.1CheckBox.Value,app.2CheckBox.Value,app.3CheckBox.Value]
end
Do I have to add this code in every callback function to be able to pass this array to the functions or is there another way of having this array always updated with the current checkbox values?
Thank you.
  4 个评论
Adam
Adam 2017-7-31
Hmm, yes, I forgot App designer is so stupid! You can do a 2nd phase initialisation with a public
function initialise( obj )
...
end
if you want, or you could create a static function to create your object which will call the constructor and then do any other stuff that you would normally do in the constructor, e.g.
methods( static, Access = public )
function obj = create( )
obj = MyApp( );
obj.checkboxes = [app.1CheckBox, app.2CheckBox, app.3CheckBox];
end
end
Yangfan Peng
Yangfan Peng 2017-7-31
编辑:Yangfan Peng 2017-7-31
I have included it in the StartupFcn and it works now, thanks! :)
Because you commented on my question, I can not accept your answer...

请先登录,再进行评论。

采纳的回答

Adam
Adam 2017-7-31
app.checkboxes = [app.1CheckBox, app.2CheckBox, app.3CheckBox];
in an initalisation function should work to store the checkboxes themselves (untested though - it works for general objects, I haven't tried with appdesigner graphics objects), then
[ app.checkboxes.Value ]
should give you the array of values at any time. Storing an array of the values is a bad idea as it will not automatically update so yes you would have to update it in every callback which would be rather pointless.
You can initialise in a few different ways, e.g.
You can do a 2nd phase initialisation with a public
function initialise( obj )
...
end
if you want, or you could create a static function to create your object which will call the constructor and then do any other stuff that you would normally do in the constructor, e.g.
methods( static, Access = public )
function obj = create( )
obj = MyApp( );
obj.checkboxes = [app.1CheckBox, app.2CheckBox, app.3CheckBox];
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by