1 view (last 30 days)
Show older comments
Benson Gou on 10 Jun 2021
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
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
What size is A?
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Stephen23 on 10 Jun 2021
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
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
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.
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
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office