Change of properties of UI dynamic components (AppDesigner)
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Caio Lorenzo Iriarte Salles
le 23 Fév 2023
Commenté : Image Analyst
le 23 Fév 2023
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
0 commentaires
Réponse acceptée
Steven Lord
le 23 Fév 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 commentaires
Image Analyst
le 23 Fév 2023
@Caio Lorenzo Iriarte Salles attach your whole .mlapp file with the paperclip icon.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!