0

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
3
  • 2
    It's not clear what you mean by an unnamed field in a structure. All fields by definition require a name. Can you include sample data in your question? Commented Jun 22, 2017 at 22:51
  • Are you certain you're dealing with a structure, and not a table or other data type? You can check the data type with class(s), where s is the variable holding your data. Commented Jun 23, 2017 at 0:20
  • My data is apparently a table inside a structure. I used the term field interchangeably with column when I meant the latter. I see where that was inappropriate particularly given the context of a matlab structure. Thanks for your assistance. Commented Jun 23, 2017 at 0:43

1 Answer 1

4

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};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.