finding a string variable in structure array
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello everyone,
I have the names of weather stations from a table read in from excel, stored in a matrix. In a separate structure array, I have data associated with the each weather station. The format of the structure array is WtrStnSDta(1).Cson(1).Nm{1, 1} , where {1,1} is where the name of the site is stored as a string variable. The name for each station is stored successively as WtrStnSDta(2)...WtrStnSDta(n).Cson(1).Nm{1,1}. The names of the stations do not appear in the same order in the two tables. Therefore I would like to write a routine to be able find the location of the station name, which is a string, in the structure array with the same name as that extracted from the first table or list. I don't know how to do this and I would like some help please. e.g. TableWithNamesOfStation = {'Rain1','Snow3', 'Wind5'};
I extract the first name as: WthStsn1 = TableWithNamesOfStation{1,1}
Then, I would like to know how to get name and data associated with the station from structure array in form: location in structure array = find in WtrStnSDta.Cson station with name = WthStsn1.
I am not sure if this makes sense but any help would be gratefully received. Thanks very much.
댓글 수: 0
답변 (1개)
KSSV
2017년 8월 23일
I hope this should be useful:
%%make my structure for demo
name = struct ;
name.Tom = 36 ;
name.Dick = 45 ;
name.Harry = 40 ;
%%Get Dick from the structure
names = fieldnames(name) ;
idx = find(not(cellfun('isempty', strfind(names,'Dick'))));
%%2016b and later
% idx = contains(names,'Tom') ;
iwant = getfield(name,names{idx})
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!