check with Matlab if object is exsting -
matlab code:
for n = 1:length(data) if exist('data{n}.sid') array{n} = data{n}.sid else array{n} = 0 end end
result:
array = [0] [0] [0] [0] [0] [0] [0]
but in first , sec struct of "data", "hk1", resp. "hk1.sid" exists , contains "1"!
how have alter code, array looks this?...
array = [1] [1] [0] [0] [0] [0] [0]
content of data:
>> info info = [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] >> data{2} ans = sid: 1 hk1: [1x1 struct] >> data{4} ans = pack_id: [1x1 struct] pack_seq_ctrl: [1x1 struct]
as see, data{2} includes "sid", data{4} doesn't include "sid"...
instead of exist('data{n}.sid')
, utilize isstruct(data{n}) && isfield(data{n}, 'sid')
.
exist
attempting check whether variable exists literally called data{n}.sid
. that's not valid variable name, returns false
, never through status wanted.
matlab
No comments:
Post a Comment