Matlab Struct Subscripted Assignment Between Dissimilar Structures

Next:Cell arraysUp:Advanced data types in Previous:Advanced data types in


A structure is a data type which contains several values, possibly of different types, referenced by name. The simplest way to create a structure is by simple assignment. For example, consider the function

The following m-file computes the value, gradient, and Hessian of f at a point x, and returns them in a structure:

function fx = f(x) fx.Value = (x(1)-1)^2+x(1)*x(2); fx.Gradient = [2*(x(1)-1)+x(2);x(1)]; fx.Hessian = [2 1;1 0]; We can now use the function as follows: >> x = [2;1] x = 2 1 >> fx = f(x) fx = Value: 3 Gradient: [2x1 double] Hessian: [2x2 double] >> whos Name Size Bytes Class fx 1x1 428 struct array x 2x1 16 double array Grand total is 12 elements using 444 bytes The potential of structures for organizing information in a program should be obvious.

Note that, in the previous example, Matlab reports as being a ``struct array''. We can have multi-dimensional arrays of structs, but in this case, each struct must have the same field names:

>> gx.Value = 12; >> gx.Gradient = [2;1]; >> A(1,1) = fx; >> A(2,1) = gx; ??? Subscripted assignment between dissimilar structures. >> fieldnames(fx) ans = 'Value' 'Gradient' 'Hessian' >> fieldnames(gx) ans = 'Value' 'Gradient' (Note the use of the command , which lists the field names of a structure.)

Beyond simple assignment, there is a command for creating structures. For information on this and other commands for manipulating structures, see .

Mark S. Gockenbach
Wed Sep 8 10:44:13 EDT 1999

As shown in the code below, I'm attempting to populate a struct array iteratively. I have a function which returns a 1x1 struct - don't worry all of the fields returned are identical across function calls - and I wish to store these output as entries in a struct array.

The problem I am running into occurs when I attempt to assign the first returned 1x1 struct to the first entry in my struct array. Below is a working dummy example of what happens:

struct_array = struct([]); first_struct = struct('field1',1,'field2',2); % This throws an error: struct_array(1) = first_struct;

Removing the index from struct_array in the last line fixes the error. This makes sense since it's overwriting (not modifying) the struct defined previously. The work around I plan to use for now is as follows:

struct_array = struct([]); for i =1:length(input) next_struct = struct_fn(input{i}) if isempty(struct_array) struct_array = next_struct; else struct_array(i) = next_struct;

However this just seems like a silly way to go about it. Is there a better way?

But further, why is this a feature?

I can understand the justification for throwing the error on indexed assignment to a 1x1 struct with no fields (the output of struct()) since the fact that it has non-zero dimensions would seem to imply that the container has been instantiated, with fields given by the empty set. So the error is thrown based on a comparison of the fields of the attempted assignment and the empty set.

However, when the struct array in question is initialized with struct([]) it is a 0x0 struct array with no fields, and so it would seem to imply that the container hasn't been instantiated and the fields are given by null. So indexed assignment should instantiate the struct and its fields.

Clearly though this is not how it works since fieldnames(struct()) and fieldnames(struct([])) both return 0x1 empty cell arrays. In fact, the only discernible difference I can find between struct() and struct([]) is that when given as input to the 'isempty' function they return (sensibly enough) true and false, respectively.

So my final question is, other than that, what's the difference between struct() and struct([])?

One thought on “Matlab Struct Subscripted Assignment Between Dissimilar Structures

Leave a Reply

Your email address will not be published. Required fields are marked *