[App Designer] Get the object indentifier that triggered the callback
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Adam
el 24 de Jul. de 2018
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.
Respuestas (3)
J. Alex Lee
el 24 de Abr. de 2020
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.
0 comentarios
Niraj Desai
el 26 de Oct. de 2020
Editada: Niraj Desai
el 26 de Oct. de 2020
You can also specify the object's "Tag" and then access the tag like this: event.Source.Tag.
2 comentarios
J. Alex Lee
el 26 de Oct. de 2020
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
el 2 de Dic. de 2020
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.
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!