我的问题是:
如果 S
是空结构,S
和 S(:)
之间有什么区别。
我认为由于这个问题而有所不同: Adding a field to an empty struct
最小说明性示例:
S = struct(); %Create a struct
S(1) = []; %Make it empty
[S(:).a] = deal(0); %Works
[S.b] = deal(0); %Gives an error
给出的错误:
当结构为空时,点名称结构分配是非法的。在上使用下标 结构。
最佳答案
事实上,这里还有一个奇怪的地方:
>> S = struct('a',{}, 'b',{})
S =
0x0 struct array with fields:
a
b
>> [S(:).c] = deal()
S =
0x0 struct array with fields:
a
b
c
>> S().d = {} %# this could be anything really, [], 0, {}, ..
S =
0x0 struct array with fields:
a
b
c
d
>> S = subsasgn(S, substruct('()',{}, '.','e'), {})
S =
0x0 struct array with fields:
a
b
c
d
e
>> S = setfield(S, {}, 'f', {1}, 0)
S =
0x0 struct array with fields:
a
b
c
d
e
f
现在有趣的是,我发现了一种让 MATLAB 崩溃的方法(在 R2013a 上测试过):
%# careful MATLAB will crash and your session will be lost!
S = struct();
S = setfield(S, {}, 'g', {}, 0)
关于matlab - 空Matlab结构S和所有元素S之间的区别(:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16342688/