From: M on
Hello there,

I've got two variables - one is a cell array containing double arrays and one is a cell array of cell arrays that contain double arrays. These two variables belong together and they are passed through a lot of functions. I thought I can simply put both together into a struct and adapted all code of my functions so they would work with the struct, as I expected it to be. But now I realized, that it looks different. My two variables look like this:
[S Z]
ans =
[1x3 double] {3x1 cell}
[2x3 double] {3x1 cell}
[4x3 double] {3x1 cell}

Is there a possibility to put them together into a 1x1 struct array, so that mystruct.S == S and mystruct.Z == Z ?

My attempt - you expect it - resulted in:

struct('S',S,'Z',Z)
ans =
3x1 struct array with fields:
S
Z
From: Steven Lord on

"M " <aflopb-mat(a)yahoo.de> wrote in message
news:hoi8rg$cig$1(a)fred.mathworks.com...
> Hello there,
>
> I've got two variables - one is a cell array containing double arrays and
> one is a cell array of cell arrays that contain double arrays. These two
> variables belong together and they are passed through a lot of functions.
> I thought I can simply put both together into a struct and adapted all
> code of my functions so they would work with the struct, as I expected it
> to be. But now I realized, that it looks different. My two variables look
> like this:
> [S Z]
> ans = [1x3 double] {3x1 cell}
> [2x3 double] {3x1 cell}
> [4x3 double] {3x1 cell}
>
> Is there a possibility to put them together into a 1x1 struct array, so
> that mystruct.S == S and mystruct.Z == Z ?
>
> My attempt - you expect it - resulted in:
>
> struct('S',S,'Z',Z)
> ans = 3x1 struct array with fields:
> S
> Z

This is the expected and documented behavior; see the "Nonscalar Struct
Arrays" subsection in this section of the documenation for structs:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04bw6-38.html#br04bw6-40

As the note at the end of that section states, wrap your cell array in a
cell array itself to store the cell in the the scalar struct. Alternately,
you could create the struct and then use field-indexed assignment to assign
the cell array into the (scalar) struct. Compare the four S* variables
below:

C = {1, 2, 3};
x = 1:10;
S1 = struct('x', x, 'C', C);
S2 = struct('x', x, 'C', {C});
S3 = struct('x', {x}, 'C', {C});
S4.x = x; S4.C = C;

whos S1 S2 S3 S4
Name Size Bytes Class Attributes

S1 1x3 752 struct
S2 1x1 532 struct
S3 1x1 532 struct
S4 1x1 532 struct

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: M on
That's it!
Thank you. I only found the function 'struct' in the documentation and well - that didn't help me. I didn't know, that there is also a so detailed docu.

M