但是,我尝试测试x
是否[]
我失败了,似乎它应该是微不足道的,但无法弄清楚如何去做。
如果我运行x = rmi('get',subsystemPath);
ans = []
我试过
x == []
x
isempty(fieldnames(x))
isEmpty(x)
但没有任何作用
function requirements = GetRequirementsFromSubsystem(subsystemPath)
x = rmi('get',subsystemPath);
if(isempty(fieldnames(x))) %%%%%%%%%%%%%%%%<------
requirements = 0;
else
requirements = {x.description}; % Fails if do this without a check
end
end
有什么想法吗?
答案 0 :(得分:0)
x
是struct
,对吗?在这种情况下,根据MATLAB新闻组的this posting,结构有两种空虚:
S = struct()
=&gt;没有字段
isempty(S)
为FALSE,因为S
是没有字段的[1 x 1]结构
S = struct('Field1', {})
=&gt;字段,但没有数据
isempty(S)
为TRUE,因为S
是带字段的[0 x 0]结构
对我来说,isempty(fieldnames(S))
仅适用于Octave中的第一个案例,至少。
另一方面,如果x
是数组而不是结构,那么isempty(x)
应该有用。
>> S = struct()
S =
scalar structure containing the fields:
>> isempty(S)
ans = 0
>> isempty(fieldnames(S))
ans = 1
>> S = struct('Field1',{})
S =
0x0 struct array containing the fields:
Field1
>> isempty(S)
ans = 1
>> isempty(fieldnames(S))
ans = 0
>> x = []
x = [](0x0)
>> isempty(x)
ans = 1