OOP member variable "properties" access from member functions

12 vues (au cours des 30 derniers jours)
Matt Reister
Matt Reister le 5 Mar 2015
Commenté : Guillaume le 6 Mar 2015
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?

Réponse acceptée

Guillaume
Guillaume le 5 Mar 2015
Modifié(e) : Guillaume le 5 Mar 2015
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

Plus de réponses (1)

Matt Reister
Matt Reister le 5 Mar 2015
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 commentaires
Matt Reister
Matt Reister le 6 Mar 2015
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?
Guillaume
Guillaume le 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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by