OOP member variable "properties" access from member functions
Show older comments
Ok I am a little new the world of OOP with MATLAB and I am trying to create a class and access the internal class properties from class member function in the same class as the data.
I was expecting to be able to refer to the properties (member data) from the member functions, but when I do it seems the member functions cannot see the class properties….
I look for a solution online and the only thing I see that talks about accessing class properties is through setter and getter functions….. Is this what I have to do to access propties from the same class as the member functions that are trying to access them? Or am I missing something?
For example if I have a class:
classdef PowerMeter
%-------------------------------
% Properties
%-------------------------------
properties (SetAccess = private)
visaObj
end
%-------------------------------
% Methods
%-------------------------------
methods (Static)
function connectToMeter(Address)
addressStr = strcat('TCPIP0::',Address,'::inst0::INSTR');
obj.visaObj = visa('agilent',addressStr);
fopen(visaObj);
end
function readPower()
fprintf(readPower.visaObj,'READ1?')
idn = scanstr(readPower.visaObj)
end
end
end
I would simply like to be able to access the property visaObj from the member functions connectToMeter() and readPower() but when I attempt to do this I get an error?
Accepted Answer
More Answers (1)
Matt Reister
on 5 Mar 2015
0 votes
4 Comments
Guillaume
on 5 Mar 2015
In matlab's context, this, obj or self are just variable names like any other. You can use whatever you want. In that respect, it's the same as python, you need to explicitly declare the object the function applies to as the first argument of the function.
While in python the convention is to call that variable self, there is no convention in matlab. Since you're familiar with python, call it self.
Guillaume
on 6 Mar 2015
"I am use to having to preface function with static"
I am not aware of any language where a static methods (or classes) would be norm.
The static concept is normally introduced much later than traditional object methods in any course on OOP.
Matt Reister
on 6 Mar 2015
Guillaume
on 6 Mar 2015
There are two nearly equivalent syntaxes for calling member functions in matlab:
object.memberfn(arg1, arg2, ...)
and
memberfn(object, arg1, arg2, ...)
Matlab convert the former into the latter (*) when it parses your code. As you can see in the second syntax, the object is actually the first argument of the function call. Matlab relies on overloading to find member functions for an object. The second syntax may actually be faster.
*: it actually converts it into
subsref(object, struct('type', {'.', '()'}, 'subs', {'memberfn', {arg1, arg2}}))
Note that you can override subsref for a class and thus redefine the . operator in matlab (leading to all sort of problems, so not recommended).
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!