Creation of app components with a loop (AppDesigner)

Hello!! I am a student and I am trying to find a way to create a good amount of lamps in my app through a 'for' loop when I press a button, where the limiting numbers are known. However, I keep having an error that says "Unrecognized property 'lampx' for class 'app1'". The code I am using is:
% Button pushed function: AplicarButton_2
function AplicarButton_2Pushed(app, event)
val_col = 80;
for i = 1:app.NfilasEditField.Value % Known number of rows
val_fila = 20;
for j = 1:app.NcolumnasEditField.Value % Known number of columns
num = i + j;
str = "lamp" + string(num);
% Creation of lamps (positions)
app.(str) = uilamp(app.UIFigure);
app.(str).Position = [val_col val_fila 18 18];
val_col = val_col + 30;
end
val_fila = val_fila + 30;
val_col = 80;
end
end
That's why I ask for an answer so I can finish my task. Thank you

 採用された回答

Simon Chan
Simon Chan 2023 年 2 月 22 日

0 投票

Try to modify
% Creation of lamps (positions)
app.(str) = uilamp(app.UIFigure);
app.(str).Position = [val_col val_fila 18 18];
to the following:
% Creation of lamps (positions)
app.Nlamp(num) = uilamp(app.UIFigure);
app.Nlamp(num).Position = [val_col val_fila 18 18];

5 件のコメント

Caio Lorenzo Iriarte Salles
Caio Lorenzo Iriarte Salles 2023 年 2 月 22 日
Hello!!
It still gives me an error, saying: "Unrecognized method, property, or field 'Nlamp' for class 'app1'". But thank you for the help!
Simon Chan
Simon Chan 2023 年 2 月 22 日
Please find the attached file for your reference.
It should be modified as following:
% Button pushed function: Button
function AplicarButton_2Pushed(app, event)
val_col = 80; % Move to here for starting value
val_fila = 20; % Move to here for starting value
for i = 1:app.NfilasEditField.Value % Known number of rows, which is 3 in the attached file
for j = 1:app.NcolumnasEditField.Value % Known number of columns, which is 5 in the attached file
num = i + j;
% Creation of lamps (positions)
app1.Nlamp(num) = uilamp(app.UIFigure);
app1.Nlamp(num).Position = [val_col val_fila 18 18];
val_col = val_col + 30;
end
val_fila = val_fila + 30;
val_col = 80;
end
end
Caio Lorenzo Iriarte Salles
Caio Lorenzo Iriarte Salles 2023 年 2 月 22 日
Okay, now the lamps are created!! Might I ask you how could I refer to each lamp following your script, so I can for example make one lamp invisible? Thank you
Simon Chan
Simon Chan 2023 年 2 月 22 日
If you would like to make lamp #4 invisible, try the following.
set(app1.Nlamp(4),'Visible','off')
Caio Lorenzo Iriarte Salles
Caio Lorenzo Iriarte Salles 2023 年 2 月 22 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by