質問

However I try to test if x is [] I fail, seems that it should be trivial, but can't figure out how to do it.

if I run x = rmi('get',subsystemPath);

ans = []

I've tried

x == []
x
isempty(fieldnames(x))
isEmpty(x)

but nothing works

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

Any ideas?

役に立ちましたか?

解決

x is a struct, right? In that case, according to this posting on the MATLAB newsgroup, there are two kinds of emptiness for structs:

  1. S = struct() => no fields

    isempty(S) is FALSE, because S is a [1 x 1] struct without fields

  2. S = struct('Field1', {}) => fields, but no data

    isempty(S) is TRUE, because S is a [0 x 0] struct with fields

For me, isempty(fieldnames(S)) works only for the first case in Octave, at least.

If x on the other hand, is an array, not a struct, then isempty(x) should work.

>> 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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top