Unable to perform assignment because dot indexing is not supported for variables of this type.
3 次查看(过去 30 天)
显示 更早的评论
Hi this is my code,
experiments(1).num = 33;
experiments(1).code = 'x';
experiments(1).weights = [200.34 202.45];
experiments(1).height.feet = 5;
experiments(1).height.inches = 6;
experiments(2).num = 11;
experiments(2).code = 't';
experiments(2).weights = [111.45 111.11];
experiments(1).height.feet = 7;
experiments(1).height.inches = 2;
I get this error whenever I run it,
Unable to perform assignment because dot indexing is not supported
for variables of this type.
Error in experimentsinitialization (line 4)
experiments(1).height.feet = 5;
When I try to call height of experiments(1), I get this,
>> experiments(1).height
ans =
5 6
However, I must get something like this,
>> experiments(1).height
ans =
feet: 5
inches: 6
What should I replace? or add?
Thank you in advance!
0 个评论
采纳的回答
Walter Roberson
2020-7-23
The code you posted is self-consistent. However, in some earlier code, you set
experiments(1).height = [5 6]
and you did not remove that.
My guess is that you modified your code along the way, but you did not clear the experiments variable so it still has the old data around.
3 个评论
Walter Roberson
2020-7-24
clear experiments
would remove that old data.
If you want to keep old data, but you want to convert height from being a vector of two elements, into being a struct with feet and inches, then
fi = experiments(1).height;
experiments(1).height = struct('feet', fi(1), 'inches', fi(2));
This would replace the entire experiments(1).height with a struct, and there is no problem replacing an entire variable.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!