필터 지우기
필터 지우기

Access the fields in a structure array specified in a list of strings.

조회 수: 1 (최근 30일)
I have a lot of numbers stored in a massive structure, and I need to access specific fields. So a minimal example, let say I have:
X.puppy=7;
X.kitten=4;
X.duckling=2;
and I want to access these fields:
furry={'puppy','kitten'};
Is there a way to do this without a loop?
I don't care what the format of the output is since I can easily convert, I just need something that outputs: [7 4].
Thanks for any help in finding my X.(furry).

채택된 답변

Image Analyst
Image Analyst 2016년 6월 16일
Try this:
X.puppy=7;
X.kitten=4;
X.duckling=2;
furry={'puppy','kitten'};
% Convert to table
t = struct2table(X)
% Extract numbers.
out = t{1, furry(:)}
  댓글 수: 1
Colin Phipps
Colin Phipps 2016년 6월 17일
Thank you. The day has come where I appreciate tables! It's possible that they're a more natural option for my data storage in the first place, I'll have to think about it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by