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

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 Comment

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.

Sign in to comment.

Answers (3)

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.
You can also specify the object's "Tag" and then access the tag like this: event.Source.Tag.

2 Comments

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.
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.

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 24 Jul 2018

Commented:

on 2 Dec 2020

Community Treasure Hunt

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

Start Hunting!