Can NOT access properties when creating a new class by inheriting the gpib class
9 views (last 30 days)
Show older comments
Hi,
I'm having a difficulty of accessing properties of my own class, inherited from the gpib class.
The purpose is to create a specific class for one lab instrument. the sample code is like:
classdef OTF920 < gpib
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties (Access = protected)
VENDOR
BOARDINDEX
PRIMARYADDRESS
end
properties (Access = public)
Wavelength
Attenuation
end
methods
function obj = OTF920(in_VENDOR, in_BOARDINDEX, in_PRIMARYADDRESS)
obj = obj@gpib(in_VENDOR, in_BOARDINDEX, in_PRIMARYADDRESS);
obj.VENDOR = in_VENDOR;
obj.BOARDINDEX = in_BOARDINDEX;
obj.PRIMARYADDRESS = in_PRIMARYADDRESS;
end
function obj = changeWL(obj, in_Wavelength)
obj.Wavelength = in_Wavelength;
disp(['Wavelength changed to ', num2str(obj.Wavelength), ' nm.']);
end
end
end
when creating an object such as
a = OTF920('ni',1,2)
I could not access any of the properties by using dot index. However, I can only access BoardIndex of the gpib class by using a code like
get(a, 'BoardIndex')
What should I do if I want to access the properties of the superclass 'gpib' and the subclass OTF920? Did I do anything wrong?
Thank you so much!
BR, Deming
2 Comments
Adam
on 17 Aug 2018
Edited: Adam
on 17 Aug 2018
I don't know anything about gpib so I don't know what its properties are and what their access specifiers are etc, but it seems a bit odd to be passing constructor arguments to the base class and then also storing them in your derived class. What does the base class do with these properties?
Also, if there some reason why the properties are 'protected'? Are you intending further subclasses?
I assume since you are subclassing it that gpib is not a Sealed class.
Answers (1)
Sean de Wolski
on 17 Aug 2018
Looking through the metaclass stack for gpib ( >>?gpib or >>metaclass(gpibobj) and looking at the metaclasses' superclasses) it doesn't look like BoardIndex is actually a property of the class.
It appears that the superclass for gpib, instrument, implements a subsref that overloads the dot indexing to grab these other fields and make them look like properties.
edit instrument.subsref
That explains what's going on. How to fix it? What exactly are you trying to change about those properties? I'd think you could add properties to your class, make them dependent, and set them to be whatever you want in the getter.
This seems like a worthwhile enhancement request so I'd recommend contacting Tech Support as well.
2 Comments
See Also
Categories
Find more on Instrument Connection and Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!