Change of properties of UI dynamic components (AppDesigner)

4 views (last 30 days)
Hello!! I am trying to develop an app that, when a first button is pressed, it creates a set of leds forming a matrix with a known number of rows and columns, and when a second button is pressed, the color of one of those leds is changed. The code described is the following:
% Button pushed function: ApplyButton
function ApplyButtonPushed(app, event)
val_col = 80; % Move to here for starting value
val_fila = 30; % Move to here for starting value
for i = 1:app.NrowsEditField.Value % Known number of rows
for j = 1:app.NcolumnsEditField.Value % Known number of columns
% Creation of lamps (positions)
led(i,j) = uilamp(app.UIFigure);
led(i,j).Color = [0 1 0];
led(i,j).Position = [val_col val_fila 18 18];
val_col = val_col + 30;
end
val_fila = val_fila + 30;
val_col = 80;
end
end
% Button pushed function: VisibleButton
function VisibleButtonPushed(app, event)
led(1,2).Color = [1 0 0]; % This command doesn't have effect
end
The problem is the second button doesn't change the color of the led. That's why I am searching for help. Thank you

Accepted Answer

Steven Lord
Steven Lord on 23 Feb 2023
You don't appear to be storing the led array you created inside ApplyButtonPushed to any of the properties of your app, so it's a local variable that gets destroyed (along with the rest of the contents of the function's workspace) when that function finishes executing. You may expect that the VisibleButtonPushed function somehow "finds" that variable from the (deleted) workspace of that ApplyButtonPushed function call but it doesn't. Instead it creates a 1-by-2 struct array each element of which has one field, Color. That struct has no connection to the array of uilamp handles you created in ApplyButtonPushed.
Give your app a property that can store the array of uilamp objects. After creating the led array in ApplyButtonPushed, assign it to that property. In VisibleButtonPushed, get the contents of that property and (after validating that it has been populated by a previous call to ApplyButtonPushed) use it to refer to the selected lamp.
  2 Comments
Caio Lorenzo Iriarte Salles
How could I create that property of storing? Sorry, but I am new in developing apps. Thank you for your answer

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!