Extracting value from Name-Value pairs using the Name string
조회 수: 11 (최근 30일)
이전 댓글 표시
Anirudha Vijay Mahagaonkar
2021년 9월 14일
댓글: Anirudha Vijay Mahagaonkar
2021년 9월 14일
Let's say I have a variable called 'test'. test is a 1x2 struct array with 2 fields - Name and Value.
test =
1×2 struct array with fields:
Name
Value
When I open the variable 'test', this is how it looks. Each of the fields have 2 values (2 pairs of name-value pair)
How can I extract just the value of the name 'long_name' without using positional index?
Using positional index, I can do it using the following code
test(2).Value
But I want to use the name string 'long_name' to find the corresponding value, as the 'long_name' may not always be at the 2nd position in other variables like test.
Any leads on how I could do that?
Thanks!
댓글 수: 0
채택된 답변
추가 답변 (1개)
Steven Lord
2021년 9월 14일
test = struct('units', {'degrees_east', 'degrees_north'}, ...
'long_name', {'longitude', 'latitude'})
theNames = {test.long_name};
findSpecificName = ismember(theNames, 'longitude');
T = test(findSpecificName)
q = test(ismember(theNames, 'latitude')).units
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!