필터 지우기
필터 지우기

how to get values from a struct

조회 수: 28 (최근 30일)
Li Xue
Li Xue 2017년 7월 14일
편집: Li Xue 2017년 7월 15일
How to get values from a struct for a list of keys?
For example, I have a struct:
my_key={'key1', 'key2', 'key3'};
value = {[1 2], [3 4], [5 6]};
s=cell2struct(value,my_key,2);
How can I extract values for query={'key1','key3'}?
s.(query) does not work. I am trying to avoid loops.

채택된 답변

Stephen23
Stephen23 2017년 7월 15일
편집: Stephen23 2017년 7월 15일
"How can I extract values for query={'key1','key3'}?"
cellfun(@(f)s.(f), query, 'uni',0)
or using getfield, something like:
getfield(s,{':'}, query{:}, {':'})

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 7월 14일
Try this:
my_key={'key1', 'key2', 'key3'}
value = {[1 2], [3 4], [5 6]}
s=struct(my_key{1}, value{1}, my_key{2}, value{2}, my_key{3}, value{3})
% Look to see if key2 is [3, 4]
if isequal(s.key2, [3,4])
msgbox('It is');
else
msgbox('It is not');
end

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by