Unable to perform assignment because dot indexing is not supported for variables of this type

Viewed 4312

When I run the code below, I get the error "

Unable to perform assignment because dot indexing is not supported for variables of this type.

" Any idea on how to fix it?

dots.nDots = 100;

for dots = 1:dots.nDots
    dots.x = (rand(1,dots.nDots));   
    dots.y = (rand(1,dots.nDots));
end
1 Answers

The line:

dots.nDots = 100;

creates the variable dots as a structure array with a field nDots. However, you overwrite the variable dots when you begin your for loop:

for dots = 1:dots.nDots

At this point, the variable dots becomes an integer value. When you then try to access the field nDots you get the error you see, because dots is no longer a structure with that field.

If you simply rename your loop variable, you will no longer get that error:

dots.nDots = 100;

for iDot = 1:dots.nDots
  dots.x = (rand(1, dots.nDots));   
  dots.y = (rand(1, dots.nDots));
end

However, it's not clear what you hope to accomplish with this loop. All this will do is repeatedly overwrite dots.x and dots.y with a new set of 100 random values each, 100 times over.

Related