Can NOT access properties when creating a new class by inheriting the gpib class

9 views (last 30 days)
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
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.
Deming Kong
Deming Kong on 17 Aug 2018
Hi Adam,
Thank you for your reply. The superclass construction function requires at least 3 inputs, as the vendor, gpib_boardindex, and gpib_address. I simply passby these inputs to store them in the user-defined subclass for testing. Sorry I did not mention this. ;-)
The problem is, I should be able to access property Wavelength, as well as Attenuation. But currently I can not. ;-(
BR, Deming

Sign in to comment.

Answers (1)

Sean de Wolski
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
Deming Kong
Deming Kong on 17 Aug 2018
Hi Sean,
Thank you for your answer! ;-)
I've also found the problem also. It seems there's a subref defined along with the instrument class. When I want to access the Wavelength property of the drived class OTF920, it gives an error:
Error using instrument/subsref (line 113)
Not enough input arguments.
However, I found the comment in the subref function
% OBJ.PROPERTY returns the property value of PROPERTY for instrument
% object OBJ.
%
% Supported syntax for instrument objects:
%
% Dot Notation: Equivalent Get Notation:
% ============= ========================
% obj.Tag get(obj,'Tag')
% obj(1).Tag get(obj(1),'Tag')
% obj(1:4).Tag get(obj(1:4), 'Tag')
% obj(1)
It seems it allows the dot index to get the properties. So it really confuses me a lot. Then I tried
properties gpib
It returns
No properties for class gpib.
I also tried the same thing with the derived class OTF920
properties OTF920
It returns the two properties in the OTF920 class
Properties for class OTF920:
Wavelength
Attenuation
which, I can not access... even with
get(a,'Wavelength')
It still generates an error
Error using instrument/get (line 81)
The name 'Wavelength' is not an accessible property for an instance of class 'GpibNI'.
I'll also try to contact Tech Support. But thank you so much for your answer!
BR, Deming
Deming Kong
Deming Kong on 17 Aug 2018
Sorry I did not make it clear.
I can only give value to the properties of OTF920 by implementing the set functions.
function obj = set.Wavelength(obj, value)
if isnumeric(value) && value>0
obj.Wavelength = value;
else
error('Wavelength must be a number')
end
end
However, still no luck in accessing them. And I want to change these properties from time to time...
Thank you again for your help!
BR, Deming

Sign in to comment.

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!