Matlab: How to access field values of a struct array

Problem: I have a structure s with the field f. I make an array of such structures:
>> s(1).f = 1;
>> s(2).f = 2;
>> s
s =
1x2 struct array with fields:
f
>> s.f
ans =
1
ans =
2

As can be seen, the answer is not a single array but rather two separate variables. As a result, this cannot be passed to other function that require an array argument, e.g. plot.

Solution: To create a variable containing the values s(:).f, just write [s.f]:
>> [s.f]
ans =
1    2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s