OOP member variable "properties" access from member functions
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
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
0 commentaires
Plus de réponses (1)
Matt Reister
le 5 Mar 2015
4 commentaires
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).
Voir également
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!