Creating objects in class with loops

7 Ansichten (letzte 30 Tage)
An Rubens
An Rubens am 10 Apr. 2020
Beantwortet: darova am 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

Akzeptierte Antwort

darova
darova am 10 Apr. 2020
I found a mistake

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by