shamalaia
shamalaia

Reputation: 2351

size of struct field from ncinfo

I want to get the size of a certain field inside a structure.

For example the size of the field Name inside the field Dimensions of the struct obtained from ncinfo:

finfo = ncinfo('example.nc');
finfo.Dimensions.Name

>>ans =

x


ans =

y


ans =

z

Just using size causes an obvious error:

size(finfo.Dimensions.Name)

Error using size Too many input arguments.

How can I do it in an alternative way?

Also, I would like to save the content of finfo.Dimensions.Name in a separate array or struct. But I get a similar error. For example:

a.b=finfo.Dimensions.Name

returns the error:

Illegal right hand side in assignment. Too many elements.

Upvotes: 1

Views: 166

Answers (2)

sco1
sco1

Reputation: 12214

Per the documentation for ncinfo, Dimensions is an array of structures, so you need to be more explicit with what you want to do.

If you want the size of the 'Dimensions' field then that is your query:

S.Dimensions(1).Name = 'x';
S.Dimensions(2).Name = 'y';
S.Dimensions(3).Name = 'z';

size(S.Dimensions)

Which returns:

ans =

     1     3

Upvotes: 2

gnovice
gnovice

Reputation: 125874

Your problem is that the Dimensions field in the structure returned by ncinfo is itself an array of structures, and when you access a field of a structure array it returns a comma-separated list of values, one for each array element. You need to collect these values, for example in a cell array:

nameCell = {finfo.Dimensions.Name};  % Now a 1-by-3 cell array of names

If you just want to know the number of dimensions, you can check the size of the Dimensions field like so:

N = size(finfo.Dimensions);

Upvotes: 1

Related Questions