user8176353
user8176353

Reputation:

Accessing Matlab structure array without field names

I have a structure with two unnamed fields that I need to access individually as vectors. The matlab help page only has examples with field names.

https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-structure-array.html

How do I retrieve an unnamed field?

Edit

For example, my data looks like this:

0.5000  0.1338
0.4999  0.1445
0.4998  0.0716

and not like:

x       y
0.5000  0.1338
0.4999  0.1445
0.4998  0.0716

Upvotes: 0

Views: 997

Answers (1)

gnovice
gnovice

Reputation: 125854

If you don't know the field names a priori, you can use fieldnames to get them, then access them using the returned values:

names = fieldnames(s);
vec1 = s.(names{1});
vec2 = s.(names{2});

Alternatively, you can ignore them altogether and just place the structure field contents in a cell array using struct2cell:

vecs = struct2cell(s);
vec1 = vecs{1};
vec2 = vecs{2};

Upvotes: 4

Related Questions