Creating objects in class with loops

10 views (last 30 days)
An Rubens
An Rubens on 10 Apr 2020
Answered: darova on 10 Apr 2020
Hi everyone, i'm trying to create objects within a class using a loop function. Therefore I'm using a function that i've already constructed for the class:
function obj = add(type,location)
obj.type = type;
obj.location = location;
end
In my main script I want to artribute objects to 'add', because there are multiple objects that need to be attributed i wanted to use a loop, that gives each object a random location within a specified field. However, in the last step (Array{ind}=add(2, [r,c])) I get the error "Unable to perform assignment because brace indexing is not supported for variables of this type.".
Array = zeros(1,Numb); % Numb is specified beforehand as a random integer
A = roadBuffer; % 2D matrix, the object can only be placed in the matrix if the value is 0
for ind = 1:Numb
B = find(A>1);
X = randi(numel(B));
[r,c] = ind2sub(size(A),B(X));
A(r, c) = 1; % change value -> no other object can be placed here
Array{ind} = add(2, [r,c]);
end

Accepted Answer

darova
darova on 10 Apr 2020
I found a mistake

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!