Suppose I have a myClass < handle with property A. If I create an instance of myClass, say myObj, and pass myObj.A to a function, say function myFunc(val), is it possible within myFunc to see that the val passed to it is a property of myObj?
EDIT: For context:
I’m writing an API (in a sense) to interface with Arduino hardware for my research lab. The overarching class is called Vehicle, with properties PinManager < handle, TelemCollector < handle, and various Device < handles. It also has methods to do things like runMotor(), getAltitude(), etc. I have a method TelemCollector.telemFetch() which is the callback for a timer event; I would like TelemCollector.telemFetch() to be able to access Vehicle methods (namely getAltitude()); naively I would just make Vehicle a property of TelemCollector to access those methods. I was hoping to not have to do this.
EDIT2: Sample code snippet of what I’m trying to accomplish:
classdef Vehicle < handle
properties
PinManager
TelemCollector
Devices
end
methods
function obj = Vehicle(PM, TC, D)
obj.TC = TelemCollector();
obj.PM = PinManager();
obj.Devices = D();
end
function val = getAltitude(obj)
%# read altitude from a connected Device
end
function val = getSpeed(obj)
%# read speed from connected Device
end
end
end
classdef TelemCollector < handle
properties
%# ...
end
methods
function fetchTelem(obj)
%# do getAltitude(), getSpeed(), etc, here.. but I want to access
%# Vehicle.getAltitude() and Vehicle.getSpeed() somehow!
end
end
end
For all I know, no.
For example if
myObj.Ais a double, myFunc will just be passed the value it contains and there will be no reference to the object. If you were callingmyFunc(somevariable)wheresomevariablewas really the name of a variable and not an expression, then callinginputname(1)inside ofmyFuncwould give you the string ‘somevariable’, but since you are referring to a property of a class, this is too complicated for MATLAB andinputname(1)just returns''(tested with MATLAB R2011a).Update: Why do you need to know this anyhow? If your interfaces are cleanly designed, you should probably not have to do this kind of thing. Or are you trying to work around someone else’s bug/bad design? Depending on your application you could think of some kind of very dirty hack involving
dbstack, trying to find out which m-file called your function, read the appropriate line of code from the .m file, parse it and then access the object usingevalin('caller',...)… but I doubt that’s a good idea ;-).Edit in response to context you provided:
Can’t you just redefine your Timer callback to hand over the “Vehicle” object as well? i.e.
means that whenever the callback timer calls the function TelemCollector.fetchTelem(), it hands over vehicle_handle as a third argument as described in the docu. This works in conjunction with a changed function head
where you can replace
eventby~in newer MATLAB versions if you don’t need it.Could that work?