[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 CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by