user807566
user807566

Reputation: 2866

Setting Properties In Object Array From Array

classdef MyObj
   properties
      A;
   end
end

%%

len = 5;
objArray = MyObj.empty(len,0);
for i=1:len
   objArray(i) = MyObj();
end

dataArray = [1 2 3 4 5];
% How do I set objArray.A to the values in dataArray?

Quick question that is basically on Matlab semantics. How can I set each objArray.A value based on the index in dataArray (without looping)?

I have tried multiple variations of [objArray.A], objArray(:).A, objArray.A(:), etc. but can't get it working.

PS: The language I'm using is MATLAB.

Thanks.

Upvotes: 2

Views: 76

Answers (1)

Huguenot
Huguenot

Reputation: 2447

If I understand what you're doing, you can assign multiple values to an object array in the following way:

values = num2cell(dataArray)
[objArray.A] = values{:}

>> objArray(1).A

ans =

     1

>> objArray(2).A

ans =

     2

>> objArray(3).A

ans =

     3

Hope this helps!

Upvotes: 3

Related Questions