App designer - How to index multiple variable names?

2 views (last 30 days)
Hello, i am trying to build a game of Mastermind in matlab app designer.
I have 40 lamps arrainged in 4 columns, and i would like to put them into a cell matrix to referance them more easily to manipulate them. This way i would only have 6 if statements instead of 40 just for changing their color, but i cannot figure out how to do this.
Making a global variable lamps = {lamp_1, lamp_2, lamp_3 etc. or {app.lamp_1, app.lamp_2, etc. doesn't seem to work.
I've tried naming them L101, L102, L201, L202 and trying app.(['L'+num2str('index')]).color = app.white; to no avail.
Anyone know how to do this better?

Accepted Answer

Mohammad Sami
Mohammad Sami on 10 Mar 2021
You should add a private property called AllLamps. Then add startupFcn to define this property.
% Code that executes after component creation
function startupFcn(app, varargin)
app.AllLamps = {app.Lamp app.Lamp2 app.Lamp3 app.Lamp4;...
app.Lamp5 app.Lamp6 app.Lamp7 app.Lamp8};
end
The you can use it to change the "Color" property as follows.
function ButtonPushed(app, event)
row = 2;
col = 2;
app.AllLamps{row,col}.Color = 'red';
end

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!