embert
embert

Reputation: 7602

What are brackets `[]` doing in Matlab, if not filled with numbers?

What are brackets [] doing in Matlab, if not filled with numbers?

Let's assume we have some objects obj1, obj2 and obj3 of a ClassA. Apparently it's possible to combine them with brackets in a .. don't know, what it actually is

objects = [obj1 obj2 obj3];

>> class(objects)

ans =

ClassA

>> objects

objects = 

  1x3 ClassA handle

  Properties:
    name
    ...
  1. Whats this called?
  2. How to iteratively build such thing?
    objects = []; objects(end+1) = current_obj; does not work
    objects{end+1} = current_obj; creates a cell
  3. How can one convert to this e. g. from a cell with objects?

When using the [] notation again on a field it gives

K>> [objects.name]

ans =

Object1Object2Object3

K>> class([objects.name])

ans =

char

Upvotes: 4

Views: 3653

Answers (4)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Square brackets are for concatenation

If you have square brackets with elements between them, you are concatenating the elements.

The elements can abe scalars, strings, vectors, matrices and so on.

Example

Assuming that the name fields of the objects struct contains a string, you can concatenate all of them like so:

[objects.name]

The result will be:

[objects(1).name objects(2).name ... objects(end).name]

Upvotes: 0

Sam Roberts
Sam Roberts

Reputation: 24127

[obj1 obj2 obj3] is an array of objects of class ClassA, just like [1 2 3] is an array of numbers.

If you type a = []; a(2) = 1, MATLAB will return a as [0 1], in other words it will fill any unspecified elements of a with a default element which, in the case of numbers, is zero.

When you type objects = []; objects(2) = current_obj, MATLAB similarly attempts to put current_obj in the requested position 2 of objects, and then to fill the unspecified elements with default objects of class ClassA. To do this, it calls the constructor of ClassA, but you need to know that it calls the constructor with no input arguments.

Therefore, if you want to be able to support this sort of array filling with objects of your class, you need to implement the class constructor so that it will not error when called with zero input arguments. For example, you could simply check nargin, and if it's zero, supply some default inputs, otherwise accept whatever inputs were provided.

By the way, by default [] is of class double. If you want to create an empty array of class ClassA, you can use objects = ClassA.empty. empty is a built-in method of all MATLAB classes. You may find that you avoid some errors by making sure that you don't accidentally attempt to concatenate doubles with objects of class ClassA.

Upvotes: 8

Kousha
Kousha

Reputation: 36219

What you are doing is creating structures. What I would do is create the same structure for objects and then overwrite the 1st index with your first object, and then iterate through the rest:

% Assume we have classA.m file available
obj1 = classA();
obj2 = classA();
obj3 = classA();

objects = obj1;
objects(end+1) = obj2;
objects(end+1) = obj3;

Upvotes: 0

Daniel
Daniel

Reputation: 36710

If you really need an empty array of objects, use some object you have and index "nothing" (from 2 to 1):

x=obj1(2:1)

Result is an empty array with a matching type. Here you can append using x(end+1). Alternatively you can use similar code to append. If x does not exist it will be created with a matching type.

if exist('x','var')
   x(end+1)=obj
else
   x(1)=obj
end

Upvotes: 0

Related Questions