I'd like to declare an empty vector that accepts insertion of user-defined types. In the following examples node
is a type I've defined with classdef node ...
The following code is rejected by the Matlab interpreter because the empty vector is automatically initialized as type double, so it can't have a node
inserted into it.
>> a = [];
>> a(1) = node(1,1,1);
The following error occurred converting from node to double:
Conversion to double from node is not possible.
The code below is accepted because the vector is initialized with a node
in it, so it can later have nodes inserted.
>> a = [node(1,1,1)];
>> a(1) = node(1,2,1);
However, I want to create an empty vector that can have nodes inserted into it. I can do it awkwardly like this:
>> a = [node(1,1,1)];
>> a(1) = [];
What's a better way? I'm looking for something that declares the initial type of the empty vector to be node
. If I could make up the syntax, it would look like:
>> a = node[];
But that's not valid Matlab syntax. Is there a good way to do this?
Empty object can be created by
A = MyClass.empty;
It works with your own class, but also with Matlab's class such as
A = int16.empty;
This method is able to create multi-dimensional empty objects with this syntax
A = MyClass.empty(n,m,0,p,q);
as long as one dimension is set to zero.
See the doc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With