Programmatically event in App Designer, is it possible?

63 vues (au cours des 30 derniers jours)
Eric Delgado
Eric Delgado le 20 Sep 2022
Commenté : Adam Danz le 21 Sep 2022
I am trying to create an event programmatically in App Designer, like a "PushedButton" or a "ValueChanged". Is it possible? I know that I can call the callback function directly, without passing the "event" argument, but I wish I could use notify function.
notify(app.myButton, 'ButtonPushed')
% Error using matlab.ui.control.Button/notify
% Cannot notify listeners of event 'ButtonPushed' in class 'matlab.ui.control.Button'.
  2 commentaires
Adam Danz
Adam Danz le 21 Sep 2022
Why not just call the ButtonPushed callback function directly?
Eric Delgado
Eric Delgado le 21 Sep 2022
Modifié(e) : Eric Delgado le 21 Sep 2022
Hey @Adam Danz, in a big project I prefer to create just one callback for several buttons (of the same family), using event.Source. If a create a "shortcut" for one of those buttons (an image in a mosaic, for example), I wish I could create an event programmatically.
function ButtonsPushedCallback(app, event)
switch event.Source
case app.Button1; % Execution code related to Button1
case app.Button2; % Execution code related to Button2
case app.Button3; % Execution code related to Button3
case app.Button4; % Execution code related to Button4
end
end
function Button1ShortcutCallback(app, event)
notify(app.Button1, 'ButtonPushed')
end

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 21 Sep 2022
Modifié(e) : Adam Danz le 21 Sep 2022
For push buttons, the event is a ButtonPushedData object with properties Source (button handle) and EventName ('ButtonPushed'). Your switch/case matches the Source with known button handles.
From what I understand, you want to directly call the ButtonsPushedCallback function and provide a known button handle so you can run some code that belongs to one of the cases. If that's correct, when you call ButtonsPushedCallback, if you have access to the app object, you can provide the button handle within a structure with a field named Source.
Example:
S = struct();
S.Source = app.Button1;
ButtonsPushedCallback(app, S)
  2 commentaires
Eric Delgado
Eric Delgado le 21 Sep 2022
Wow @Adam Danz, thank you! That's one of the kinds of answers that you think "It's obvious!". A simple and clever solution. It works! And now I just deleted more than 10 callbacks. :)
It's important to put true in the Value property of the button, so it could behavior like a "button clicked".
function ButtonsPushedCallback(app, event)
switch event.Source
case app.Button1; % Execution code related to Button1
case app.Button2; % Execution code related to Button2
case app.Button3; % Execution code related to Button3
case app.Button4; % Execution code related to Button4
end
end
function Button1ShortcutCallback(app, event)
% notify(app.Button1, 'ButtonPushed') % It doesn't work!
app.Button1.Value = 1;
ButtonsPushedCallback(app, struct('Source', app.Button1))
end
Adam Danz
Adam Danz le 21 Sep 2022
Glad it worked out! You could simplify my answer to create the structure and the field within the same line:
S = struct('Source', app.Button1);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by