[英]Unable to perform assignment because dot indexing is not supported for variables of this type
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
The line: 这条线:
dots.nDots = 100;
creates the variable dots
as a structure array with a field nDots
. 将变量
dots
创建为具有字段nDots
的结构数组 。 However, you overwrite the variable dots when you begin your for loop : 但是,在开始for循环时,您会覆盖变量点:
for dots = 1:dots.nDots
At this point, the variable dots
becomes an integer value. 此时,可变
dots
变为整数值。 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. 当您尝试访问字段
nDots
您会看到错误,因为dots
不再是具有该字段的结构。
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. 所有这一切都会重复覆盖
dots.x
和dots.y
,每组100个随机值,100次以上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.