tags:

views:

790

answers:

4

I have no idea what's going on here. I'm using R2006b. Any chance someone out there with a newer version could test to see if they get the same behavior, before I file a bug report?

code: (bug1.m)

function bug1
S = struct('nothing',{},'something',{});
add_something(S, 'boing');          % does what I expect
add_something(S.something,'test');  % weird behavior
end

function add_something(X,str)
    disp('X=');
    disp(X);
    disp('str=');
    disp(str);
end

output:

>> bug1
X=
str=
boing
X=
test
str=
??? Input argument "str" is undefined.

Error in ==> bug1>add_something at 11
    disp(str);

Error in ==> bug1 at 4
add_something(S.something,'test');

commentary:

It looks like the emptiness/nothingness of S.something allows it to shift the arguments for a function call. This seems like Very Bad Behavior. In the short term I want to find away around it (I'm trying to make a function that adds items to an initially empty cell array that's a member of a structure).

edit:

correlary question: so there's no way to construct a struct literal containing any empty cell arrays?

+1  A: 

Output is identical in R2008b:

>> bug1
X=
str=
boing
X=
test
str=
??? Input argument "str" is undefined.

Error in ==> bug1>add_something at 11
    disp(str);

Error in ==> bug1 at 4
add_something(S.something,'test');  % weird behavior
Ben Schwehn
thanks..........
Jason S
+1  A: 

ARGH... I think I found the answer. struct() has multiple behaviors, including:

Note If any of the values fields is an empty cell array {}, the MATLAB software creates an empty structure array in which all fields are also empty.

and apparently if you pass a member of a 0x0 structure as an argument, it's like some kind of empty phantom that doesn't really show up in the argument list. (that's still probably a bug)

bug2.m:

function bug2(arg1, arg2)
disp(sprintf('number of arguments = %d\narg1 = ', nargin));
disp(arg1);

test case:

>> nothing = struct('something',{})

nothing = 

0x0 struct array with fields:
    something

>> bug2(nothing,'there')
number of arguments = 2
arg1 = 
>> bug2(nothing.something,'there')
number of arguments = 1
arg1 = 
there
Jason S
+2  A: 

This behaviour persists in 2008b, and is in fact not really a bug (although i wouldn't say the designers intended for it): When you step into add_something(S,'boing') and watch the first argument (say by selecting it and pressing F9), you'd get some output relating to the empty structure S. Step into add_something(S.something,'test') and watch the first argument, and you'd see it's in fact interpreted as 'test' !

The syntax struct.fieldname is designed to return an object of type 'comma separated list'. A functions in matlab is designed to receive an object of the that exact type: the argument names are given to the values in the list, in the order they are passed. In your case, since the first argument is an empty list, the comma-separated-list the function receives starts really at the second value you pass - namely, 'test'.

Ofek Shilon
Actually, the designers *did* intend for this behavior. The syntax for passing arguments to the STRUCT function is designed such that you can create either a structure containing arrays or an array of structures, depending on your use of cell array encapsulation of the input arguments.
gnovice
Also, the comma-separated list (CSL) behavior is intended as well. Input and output argument lists are designed as CSLs, and the following syntaxes create a CSL: structureArray.fieldName, cellArray{:}.
gnovice
Of course both the behaviours you describe are by design. However, as just seen - a situation where several arguments are passed to a function and one in the middle is an empty CSL, entails consequences of this design that are *sure* to cause confusion. Such a scenario (seems to me) justifies separate design attention. At least, say, raise a runtime warning.
Ofek Shilon
+6  A: 

As you already discovered yourself, this isn't a bug but a "feature". In other words, it is the normal behavior of the STRUCT function. If you pass empty cell arrays as field values to STRUCT, it assumes you want an empty structure array with the given field names.

>> s=struct('a',{},'b',{})

s = 

0x0 struct array with fields:
    a
    b

To pass an empty cell array as an actual field value, you would do the following:

>> s = struct('a',{{}},'b',{{}})

s = 

    a: {}
    b: {}

Incidentally, any time you want to set a field value to a cell array using STRUCT requires that you encompass it in another cell array. For example, this creates a single structure element with fields that contain a cell array and a vector:

>> s = struct('strings',{{'hello','yes'}},'lengths',[5 3])

s = 

    strings: {'hello'  'yes'}
    lengths: [5 3]

But this creates an array of two structure elements, distributing the cell array but replicating the vector:

>> s = struct('strings',{'hello','yes'},'lengths',[5 3])

s = 

1x2 struct array with fields:
    strings
    lengths

>> s(1)

ans = 

    strings: 'hello'
    lengths: [5 3]

>> s(2)

ans = 

    strings: 'yes'
    lengths: [5 3]
gnovice
AH: thanks -- I didn't see the notes in the struct function that talk about using extra braces whenever you want to pass in a cell array. Grrr.
Jason S