App designer - How to index multiple variable names?

10 ビュー (過去 30 日間)
Zane Winyard
Zane Winyard 2021 年 3 月 10 日
コメント済み: Zane Winyard 2021 年 3 月 16 日
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?

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 3 月 10 日
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
  1 件のコメント
Zane Winyard
Zane Winyard 2021 年 3 月 16 日
Thanks a lot! Cut 1000+ if statements down to about 8 lines lol

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by