Say I have a MATLAB object defined in a class file
classdef foo
properties
bar
end
end
And I create a foo object
myfoo = foo();
Now I want to add another field to foo dynamically. What I want is
myfoo.newfield = 42;
but this will throw an error.
I know there is a way to dynamically add a field/property to a MATLAB object but I can't remember it or find it easily in the help. Anyone know the syntax?
Ok, found it. But it's not general, only subclasses of the dynamicprops class implement it.
This is what I remember coming across. So I suspect the general answer to this question is you can't do it.
Any class that is a subclass of the dynamicprops class (which is itself a subclass of the handle class) can define dynamic properties using the addprop method. The syntax is:
P = addprop(H,'PropertyName')
Related
Apologize if the title is confusing.
I tried to set an attribute of a class as another class:
classdef gun
properties
bullets;
...
Where bullets is later initialized as another class object bullets = bul(10);
classdef bul
properties
...
methods
function obj = addBullet(obj, num)
...
But when I tried to invoke method addBullet in class gun, like gun.bullets.addBullet(2), I got an error saying:
Dot indexing is not supported for variables of this type.
Is it because Matlab does not support class as another class' attribute? Any way I can fix that?
It is my first time asking a question here and I am quite new to MATLAB. So, I am sorry in advance if my explanation is faulty or insufficient. I'll be happy to hear any advice to improve myself.
I would like to create a class which has an array of a certain size. Let's call this class 'MyClass'. My class is as follows:
classdef MyClass
properties
A = array2table(zeros(ArraySize,1));
end
end
The variable ArraySize is defined in my main.m file and i want to create an object from this class within the same file:
ArraySize = 10;
MyObject = MyClass;
However, the class I created does not recognize the ArraySize variable. Could somebody tell me if there is an easy way to achieve this?
So far, I tried to make it a global variable, I tried using 'load' function to pass parameters between files. I tried defining the class inside a function. None of them seem to work. I read about 'handles' in forums and i got the idea that it somehow can be related to the solution of my problem, but I do not really know how to work with them. What I understood so far is that handles correspond to pointers in C++. I would like to know if they can be used to solve my problem and if so, how exactly.
Thanks in advance.
You should just write your constructor to accept ArraySize as an input argument and then initialize the value A inside of your constructor.
classdef MyClass
properties
A
end
methods
function self = MyClass(arraySize)
self.A = array2table(zeros(arraySize,1));
end
end
end
And then instantiate your class
myObject = MyClass(ArraySize);
And with regards to handle classes, check out this page of the documentation to see recommendations on when to use handle and value classes.
I defined a class in one of my many MATLAB packages. To my suprise, I could not access a constant property of my class without importing the class definition. Even if it is a method of the class itself. Like so:
classdef TestClass
properties( Constant )
c=0;
end
methods
function obj = TestClass()
end
function getC(obj)
import test.TestClass;
disp(TestClass.c);
end
end
end
I just want to check whether I am doing something wrong here or is this the correct way to use constants in MATLAB.
Since you have placed TestClass inside a package Matlab needs to know where to look to find the definition for this class, even if it's a reference from within the class or function. An alternate to the above code could be:
function getC(obj)
disp(test.TestClass.c);
end
Alternately, if within a class, constant values can be accessed from the object itself.
function getC(obj)
disp(obj.c);
end
If neither of these is working for you, you may need to refresh the classdef for TestClass from memory. This will cause matlab to reload the constant value, which is pulled into Matlab when it first parses the classdef file to determine the structure of the class. This can be done using clear classes, however a warning that it will also clear all other classes, variables, and any breakpoints you have set.
If you want to see if this is necessary you can view the metaclass object to determine what Matlab "thinks" your class structure should be. You can do this using the following.
mc = ?test.TestClass;
mc.PropertyList
You may need to index into the property list to find the specific property you are interested in, but the thing you are looking for are the following fields.
Name: 'c'
Constant: 1
DefaultValue: 0
Pretty new to matlab. I want to have a class, which does some calculation. I want to import this class in another class( not instantiate). and use the functions as default functions.
This did not help me much. Can we import a user defined class/functions?
So you have a class calculationClass, and you want to create another class otherClass that can access the calculations provided by calculationClass
One way that works if the calculations are either normal or static methods would be to subclass calculationClass, i.e. start your class definition with
classdef otherClass < calculationClass
[some code here]
end
This way, all methods of calculationClass immediately become available to otherClass. Note that if calculationClass has a nonempty constructor, the subclass will call the constructor as this = this#calculationClass.
If the calculations are static methods only, you can, alternatively, access those calculations as calculationClass.someCalculation(inputArguments), or create a package and use import.
I have had trouble finding help in the matlab documentation and previous questions about using matlab inheritance and class constructors to make an interface. To make it tidy, within a package.
Instead of dragging through my code I can condense it as follows:
A package +MyPkg has a superclass Super and a few subclasses Sub1 Sub2... Most of my properties and methods are defined in Super such that Sub1 and Sub2 really only exist to use their constructors for simple routines or perhaps a few methods overloaded from Super.
So how do I go about writing the classdefs and constructors to support an interface where I can use the following calls:
a = MyPkg.Super(args).Sub1(args)
b = MyPkg.Super(args).Sub1(args).Sub1Method
In this case I want to keep arguments related to Super apart from arguments related to Sub1 for readability and organization.
Questions are welcome.
EDIT:
After considering the accepted answer below and some browsing I reached the conclusion that the interface shown above is not really in the spirit of OO and, for my data analysis application of it a more proper way to approach it would consist of a handle class with a constructor that populates an object or cell array of object properties. Because the class is a handle class one can then use the methods on it to produce desired methods. i.e. the following
% in +MyPkg\
classdef Super < handle
properties
outputArray
end
methods
function self = Super(args)
self.outputArray=load_values(args);
end
function out = do_analysis(self,params)
% do some analysis
end
end
end
Then to use this:
data1 = MyPkg.Super(args)
% Populate the outputArray
analysis1 = data1.do_analysis(params)
etc.,
Hope that helps someone else dealing with these issues
With respect to your question, you can't if you use inheritance. Only direct superclass constructors can be called from subclasses and only from the subclass can you call the superclass constructor. Ref.
Exposing the superclass like that really breaks the fundamentals of inheritance. Maybe ou should be thinking of another model, maybe composition ("has a" instead of "is a"), if you need that kind of access?