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

64 views (last 30 days)
When I run this code, 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? Thank you very much
dots.nDots = 100; % number of dots
for dots = 1:dots.nDots
dots.x = (rand(1,dots.nDots));
dots.y = (rand(1,dots.nDots));
end

Accepted Answer

Stephen23
Stephen23 on 17 Jun 2019
Edited: Stephen23 on 17 Jun 2019
You re-defined the variable name for the loop iterator variable:
dots.nDots = 100;
%^^^ variable name.
for dots = 1:dots.nDots
% ^^^^ same variable name!!
So as soon as the loop starts, the variable dots refers to a numeric scalar, e.g. 1. Numeric variables do not have any methods or properties that can be accessed using dot notation.
If you want to refer to the variable that you defined before the loop then you need to use a different name for the loop iterator variable, not reuse that same name as that variable.
Note that your code has some other bugs, but it is not clear what you expect the output to be (you did not explain this). I suspect that you do not need the loop at all:
dots.nDots = 100;
dots.x = rand(1,dots.nDots);
dots.y = rand(1,dots.nDots);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!