OOP member variable "properties" access from member functions

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

You've made two mistakes with your code.
The first one, not specific to matlab is that static methods don't apply to to objects ofthe class but are generic to the class. So first thing is to remove the static attribute from your methods.
The second error, specific to matlab is that unlike most other languages the object the method applies to must be specified as the first argument of the function.
Therefore your method block should be:
methods
function connectToMeter(this, Address) %I prefer calling the object this (as in C++/C#) rather than obj
addressStr = sprintf('TCPIP0::%s::inst0::INSTR', Address);
this.visaObj = visa('agilent', addressStr);
fopen(this.visaObj);
end
function readPower(this)
fprintf(this.visaObj, 'READ1?');
idn = scanstr(this.visaObj);
end
end

More Answers (1)

Ahh,
Thanks!
I didn't even notice i had the static keyword in there.. i am use to having to preface function with static...
So using the this "keyword" as the first argument worked.
just out of curiosity is "this" a keyword in MATLAB? you mentioned that you could use "obj" as well? are these two equivalent?
I guess this is like python and "self"....
Thanks a Million

4 Comments

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.
"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.
what i meant was in c++ when you declare a function static you go:
static void function();
where it looks like in MATLAB you declare a group of functions (methods) static???
However, i still think i am not understanding something. If this,obj,self are not keywords in MATLAB and simply a regular variable then how does MATLAB now this variable refers to the object?
Does it simply assume that the first argument passed to a function is the object?
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).

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!