开发者

MATLAB - obtain the object a property belongs to?

开发者 https://www.devze.com 2023-04-01 11:53 出处:网络
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

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.A is a double, myFunc will just be passed the value it contains and there will be no reference to the object. If you were calling myFunc(somevariable) where somevariable was really the name of a variable and not an expression, then calling inputname(1) inside of myFunc would give you the string 'somevariable', but since you are referring to a property of a class, this is too complicated for MATLAB and inputname(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 using evalin('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.

set(yourtimer_handle,'TimerFcn',{@fetchTelem,vehicle_handle});

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

function fetchTelem(obj, event, vehicle_handle)

where you can replace event by ~ in newer MATLAB versions if you don't need it.

Could that work?


fetchTelem can't call methods of an object that it doesn't have a reference to. So, regardless, you need to provide your TelemCollector object with the Vehicle handle.

Personally, I think the association between Vehicle and TelemCollector should be in the opposite direction. I would prefer something that looked more like:

V = Vehicle(PM, D);
TC = TelemCollector(V);

Although it really depends on how you expect to use the classes.

I agree with @Jonas Heidelberg: if it's this difficult, then it's probably the wrong interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消