Change of properties of UI dynamic components (AppDesigner)

5 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2023-2-23
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by