然而,我试着测试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有什么想法吗?
发布于 2013-07-23 15:15:57
x是struct,对吗?在这种情况下,根据MATLAB新闻组上的本帖,对于结构有两种类型的空化:
S = struct() =>无字段
isempty(S)是假的,因为S是一个没有字段的1x1结构。S = struct('Field1', {}) =>字段,但没有数据
isempty(S)为真,因为S是带字段的0 x 0结构。至少对我来说,isempty(fieldnames(S))只适用于八达夫的第一种情况。
另一方面,如果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 = 1https://stackoverflow.com/questions/17813934
复制相似问题