How to extract some values from a structure? (2024)

1 view (last 30 days)

Show older comments

Benson Gou on 10 Jun 2021

  • Link

    Direct link to this question

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure

  • Link

    Direct link to this question

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure

Commented: Benson Gou on 10 Jun 2021

Accepted Answer: Stephen23

Dear All,

I have a structure A which has several fields. Each field has 100 values. I want to extract the first 20 values from each field and save them in a structure B. I tried to use the following code:

B = A(1:20).

It does not work. Would you please tell me how I can get structure B?

Thanks.

Benson

1 Comment

Show -1 older commentsHide -1 older comments

Stephen23 on 10 Jun 2021

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#comment_1574395

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#comment_1574395

What size is A?

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Stephen23 on 10 Jun 2021

  • Link

    Direct link to this answer

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#answer_721415

  • Link

    Direct link to this answer

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#answer_721415

Assuming that structure A is scalar (so far you did not tell us this important information):

A.x = randi(9,1,100);

A.y = randi(9,1,100);

A.z = randi(9,1,100)

A = struct with fields:

x: [4 9 2 1 1 1 7 2 2 2 9 2 7 4 9 7 4 8 8 1 3 4 1 7 1 8 6 5 2 3 3 5 7 3 3 8 3 8 8 6 7 9 7 2 9 2 3 4 1 6 4 6 7 9 2 6 7 9 1 7 6 7 2 9 5 5 6 8 3 5 3 2 1 7 9 3 4 1 1 6 1 8 1 8 5 3 5 3 1 1 8 5 7 2 7 5 6 9 9 4] y: [8 9 7 2 1 9 6 6 7 1 9 9 6 7 1 8 2 5 5 4 7 7 7 2 6 6 6 3 6 4 6 1 6 1 6 2 3 2 5 5 1 7 5 2 9 5 6 5 6 8 7 1 6 8 9 5 4 8 5 4 8 8 8 6 3 8 2 5 4 6 6 2 6 4 4 5 4 8 3 8 3 8 6 5 3 3 6 5 7 8 2 1 1 6 7 2 3 5 3 6] z: [8 9 8 7 3 5 4 8 4 3 2 3 2 2 6 1 4 3 4 8 5 9 4 1 6 1 2 5 9 5 2 2 2 3 3 2 6 1 2 4 8 5 2 4 8 3 5 1 7 3 3 5 5 9 1 9 8 9 5 1 1 1 1 6 5 4 2 3 1 9 8 1 8 1 3 8 5 5 9 7 5 5 6 2 7 5 2 8 3 2 9 8 1 7 5 2 9 6 6 6]

fun = @(v)v(1:20);

B = structfun(fun,A,'uni',0)

B = struct with fields:

x: [4 9 2 1 1 1 7 2 2 2 9 2 7 4 9 7 4 8 8 1] y: [8 9 7 2 1 9 6 6 7 1 9 9 6 7 1 8 2 5 5 4] z: [8 9 8 7 3 5 4 8 4 3 2 3 2 2 6 1 4 3 4 8]

1 Comment

Show -1 older commentsHide -1 older comments

Benson Gou on 10 Jun 2021

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#comment_1575520

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/852600-how-to-extract-some-values-from-a-structure#comment_1575520

Thanks a lot. It works well.

Benson

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

SciencesEarth, Ocean, and Atmospheric SciencesOceanography and Hydrology

Find more on Oceanography and Hydrology in Help Center and File Exchange

Tags

  • extract
  • structure

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to extract some values from a structure? (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

Europe

Asia Pacific

Contact your local office

How to extract some values from a structure? (2024)

References

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5520

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.