[App Designer] Get the object indentifier that triggered the callback

39 ビュー (過去 30 日間)
Miguel Garcia
Miguel Garcia 2018 年 7 月 24 日
コメント済み: Dominik Müller 2020 年 12 月 2 日
I have several objects such spinners that execute a callback. I could create a callback for each one of them, but I want to write less lines in the app to make it clean. My question is if there is any way to identify which object triggered the callback.
% Value changed function: ASpinner, BSpinner, CSpinner
function VChanged(app, event)
% I want here somthing like if event.Source.Name==ASpinner
do that
end
end
event.EventName is always 'ValueChanged', i tried with the event.Source, but those are the properties I am able to see and none refrer to the name of the spinner.
Spinner (1.75) with properties:
Value: 1.7500
ValueDisplayFormat: '%11.4g'
RoundFractionalValues: 'off'
Step: 0.2500
Limits: [0 5]
LowerLimitInclusive: 'on'
UpperLimitInclusive: 'on'
ValueChangedFcn: [function_handle]
ValueChangingFcn: ''
Position: [114 12 80.8954 22]
Use get to show all properties
Step: 0.2500
ValueChangingFcn: ''
Value: 1.5000
Limits: [0 5]
LowerLimitInclusive: 'on'
UpperLimitInclusive: 'on'
RoundFractionalValues: 'off'
ValueDisplayFormat: '%11.4g'
ValueChangedFcn: [function_handle]
Parent: [1×1 Panel]
HandleVisibility: 'on'
BusyAction: 'queue'
BeingDeleted: 'off'
Interruptible: 'on'
CreateFcn: ''
DeleteFcn: ''
Type: 'uispinner'
Tag: ''
UserData: []
Editable: 'on'
HorizontalAlignment: 'right'
FontName: 'Helvetica'
FontSize: 14
FontWeight: 'normal'
FontAngle: 'normal'
FontColor: [0 0 0]
BackgroundColor: [0.9373 0.9373 0.9373]
InnerPosition: [114 12 80.8954 22]
Position: [114 12 80.8954 22]
OuterPosition: [114 12 80.8954 22]
Enable: 'on'
Visible: 'on'
PS: while I could use event.Source.Position, if the user resize the app is not gonna work. Thanks in advance
  1 件のコメント
Adam
Adam 2018 年 7 月 24 日
event.Source should be the handle of the component that triggered the callback. If you need to know which of your properties it is you can just test equality of e.g.
event.Source == app.ASpinner
as far as I am aware, though I rarely use AppDesigner.

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

回答 (3 件)

Miguel Garcia
Miguel Garcia 2018 年 7 月 24 日
So obvious and I didnt saw it ! Thank you very much!

J. Alex Lee
J. Alex Lee 2020 年 4 月 24 日
I think a cleaner way is to pass an identifier with the callback definition:
ASpinner.ValueChangedFcn = @(evnt)app.VChanged(evnt,'ASpinner')
BSpinner.ValueChangedFcn = @(evnt)app.VChanged(evnt,'BSpinner')
CSpinner.ValueChangedFcn = @(evnt)app.VChanged(evnt,'CSpinner')
function VChanged(app, evnt,SpinnerName)
source = app.(SpinnerName)
if SpinnerName=='ASpinner'
end
switch SpinnerName
case 'ASpinner'
end
end
Alternatively, make use of the "Tag" or "UserData" properties of the spinner to store the name in.

Niraj Desai
Niraj Desai 2020 年 10 月 26 日
編集済み: Niraj Desai 2020 年 10 月 26 日
You can also specify the object's "Tag" and then access the tag like this: event.Source.Tag.
  2 件のコメント
J. Alex Lee
J. Alex Lee 2020 年 10 月 26 日
yes, i agree. the usefulness would probably depend on whether you need to query the spinner's user-facing identity in any other context than the ValueChanged callback.
Dominik Müller
Dominik Müller 2020 年 12 月 2 日
Is there a way to have two identifiers? Let's say Tag and Name for example? I'm asking because I have a structure containing names different than the Spinner names. It would be nice to get two identifiers to easily access both of them.

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

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by