Creating objects in class with loops

조회 수: 4 (최근 30일)
An Rubens
An Rubens 2020년 4월 10일
답변: darova 2020년 4월 10일
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

채택된 답변

darova
darova 2020년 4월 10일
I found a mistake

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by