[App Designer] Get the object indentifier that triggered the callback
19 次查看(过去 30 天)
显示 更早的评论
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
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 个)
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.
0 个评论
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
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
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 Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!