
Creating objects in class with loops
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!