Creating objects in class with loops

4 visualizaciones (últimos 30 días)
An Rubens
An Rubens el 10 de Abr. de 2020
Respondida: darova el 10 de Abr. de 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

Respuesta aceptada

darova
darova el 10 de Abr. de 2020
I found a mistake

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by