필터 지우기
필터 지우기

empty cell array value lost when passed to a function as a part of an anonymous structure

조회 수: 2 (최근 30일)
Consider the following:
>> x % x is a simple two-field structure
x =
x: 1
y: 'y'
>> type('printMe.m') % printMe is just a simple function
function printMe(x)
size(x), x
>> printMe(x)
ans =
1 1
x =
x: 1
y: 'y'
>> printMe(struct('x', 1, 'y', 'y')) % Same result passing in an equivalent anonymous struct
ans =
1 1
x =
x: 1
y: 'y'
>> % But look what happens if one of the values is an empty cell array
>> printMe(struct('x', 1, 'y', {}))
ans =
0 0
x =
0x0 struct array with fields:
x
y
>> % Is this because it's empty
>> printMe(struct('x', 1, 'y', ''))
ans =
1 1
x =
x: 1
y: ''
>> printMe(struct('x', 1, 'y', []))
ans =
1 1
x =
x: 1
y: []
>> printMe(struct('x', 1, 'y', struct))
ans =
1 1
x =
x: 1
y: [1x1 struct]
>> % Apparently not!
So, apparently, if (and only if) a value in an anonymous struct passed to a function is an empty cell array, the function "loses" all the values in the anonymous struct and thinks it's empty (though, curiously, it doesn't completely "lose" the struct and in particular it retains "knowledge" of its fields, just not the values assigned to those fields).
Bug or "feature," and if the latter, rationale?
Thanks for your time,
OlyDLG

채택된 답변

Walter Roberson
Walter Roberson 2011년 7월 19일
Feature. Using a cell array in struct() is the constructor for an array structure; for example,
struct('x',{1 3}, 'y', {4 6})
produces a 1x2 struct array.
When an empty entry is used, it signals to struct() that the struct array as a whole is to be of size 0.
You can use {{}} if you want an actual empty cell at that location.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by