Struct contents reference from a non-struct array object

(Not matching to prev questions on google).I have struct of struct of arrays. When any struct is empty while concatenating, the above error is displayed.
d=[s1.a.d;s2.b.d;s3.c.d]
when any struct a,b,c is empty above error is shown. How to redefine stacking to eliminate the error.

 採用された回答

Guillaume
Guillaume 2018 年 8 月 3 日

0 投票

"when any struct a,b,c is empty"
If a is empty (i.e. a = []), then it is not a structure, and a.d doesn't mean anything.
What would be the result of the concatenation anyway in this case.

3 件のコメント

Mukund
Mukund 2018 年 8 月 3 日
This line is iterated,hence kept. On other times, that struct could be non-empty. I wanted to know: either 1) how to improve the concatenation, or 2) introduce check conditions. I have tried isempty and isstruct but they are not working as expected.
Stephen23
Stephen23 2018 年 8 月 3 日
編集済み: Stephen23 2018 年 8 月 3 日
"If a is empty (i.e. a = []), then it is not a structure, and a.d doesn't mean anything."
MATLAB clearly allows empty structures:
>> a = struct('d',{})
a =
0x0 struct array with fields:
a
>> isempty(a)
ans =
1
So the logical that something empty cannot be a structure is incorrect.
"What would be the result of the concatenation anyway in this case."
The a.d syntax always returns a comma-separated list with as many elements as a has (in this case zero):
>> a.d
>>
So the empty structure does exist, and it (correctly) returns a comma-separated list with zero things in it. I would also expect a comma-separated list from a cell array to deliver the same result:
>> C = {}
C =
{}
>> C{:}
>>
So far no surprises for me. Comma-separated lists with zero members can easily be concatenated without any error:
>> b = struct('d',2);
>> [a.d,b.d]
ans =
2
Any problems are solely due to the data type, and have nothing to do with whether the array is empty or not. Fix the data class.
Mukund
Mukund 2018 年 8 月 3 日
Thanks Stephen, you spotted the mistake accurately.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2018 年 8 月 3 日

コメント済み:

2018 年 8 月 3 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by