matlabcell-arraymatlab-struct

MATLAB "bug" (or really weird behavior) with structs and empty cell arrays


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');

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:

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


Solution

  • 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]