Subscripted assignment dimension mismatch in structure.

2 次查看(过去 30 天)
I am trying to create a structure with a lot of information and multiple layers.
Level(10).Sublevel(5).XXXXX = 'test' works just fine
However, when I try to add another sublevel such as
Level(10).Sublevel(5).Subsublevel(15).XXXXX = 'test'
I get the Subscripted assignment dimension mismatch error.
Is this a MATLAB limitation or am I doing something wrong?
  1 个评论
Geoff Hayes
Geoff Hayes 2016-7-23
At what point do you call the second line of code? If I execute the following two lines in succession
Level(10).Sublevel(5).XXXXX = 'test';
Level(10).Sublevel(5).Subsublevel(15).XXXXX = 'test';
then I don't observe any errors. Please provide more details.

请先登录,再进行评论。

回答(1 个)

Div Tiwari
Div Tiwari 2016-8-8
Was the first assignment Level(10).Sublevel(5).Subsublevel = 'test'? That makes 'Level(10).Sublevel(5).Subsublevel' a 'char' vector of length 4. As a result, indexing into 'Subsublevel' with an index greater than 4 gives a 'Field assignment to a non-structure array object' error. Using an index of 4 or less will result in a size mismatch error.
To get around this, you may reassign 'Subsublevel' as a struct before the second assignment, as follows:
>> Level(10).Sublevel(5).Subsublevel = 'test';
>> Level(10).Sublevel(5).Subsublevel = struct;
>> Level(10).Sublevel(5).Subsublevel(15).XXXXX = 'test';

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by