3

I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.

A simplified example of the problem.

classdef clsMyClass < handle
   properties 
      dblMyProperty1
   end 
   methods
        function obj = clsMyClass()
        end      
   end
end 

And when running

vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;    
vecMyArray.dblMyProperty1 = 1:100;    

We get the following error:

??? Incorrect number of right hand side elements in dot name assignment. Missing [] around left hand side is a likely cause.

Any help would be appreciated.

Ruut
  • 1,091
  • 1
  • 16
  • 29
  • My two cents: `for` loop is probably your fastest solution. Also the first line of code (`.empty(...)`) doesn't do anything and its effect is immediately replaced by the second line of code. – Bee Jun 14 '13 at 16:06
  • Thanks to MATLAB's vectorization, you're doing this "wrong". Consider turning the class "inside-out" - rather than have an array of objects, have a single object whose properties are themselves arrays. See [this treatise on MATLAB and OOP](http://stackoverflow.com/a/1745686/238644). – Dang Khoa Jun 14 '13 at 16:07
  • @DangKhoa, I plan to have only 20 objects which hold multiple large arrays. I would like to call methods of these 20 objects at once and change their properties. – Ruut Jun 14 '13 at 16:17
  • @Bee, thanks you are right. I added this line, because of reasons explained here: http://stackoverflow.com/a/7879881/903186 – Ruut Jun 14 '13 at 16:18
  • Are you initializing the objects? Why not just set the property with a default inside the class definition? – Dang Khoa Jun 14 '13 at 16:25
  • @DangKhoa, in this case initialization would not be an option, because I would like to call a few methods, change properties of all objects and call a few other methods. – Ruut Jun 14 '13 at 20:01

3 Answers3

1

You can use the deal function for exactly this purpose:

[vecMyArray.dblMyProperty1] = deal(1:100);

See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html


Edit: No you can't, actually; that'll set them to all be the vector 1:100.

chessbot
  • 436
  • 2
  • 11
1

I see what you're trying to do now. Use disperse from the MATLAB File Exchange:

>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans = 
    1
>> vecMyArray(10).dblMyProperty1
ans = 
    10
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80
  • I think this will solve my problem. Unfortunately I am using MATLAB Version 7.9.1.705 (R2009b) SP1 and this gives the error: `??? Undefined function or method 'disperse' for input arguments of type 'double'.` Therefore I can not validate your solution – Ruut Jun 18 '13 at 07:53
  • This is not a built in function, it's from the MATLAB file exchange. Hit the link to download it. – Dang Khoa Jun 18 '13 at 13:01
0

I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.

Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:

So you'll need:

for ii=1:100
  vecMyArray(ii).dblMyProperty1 = ii;
end

I know it's not satisfying, but I think it at least helps us to definitively understand this error.

macduff
  • 4,655
  • 18
  • 29