finding a string variable in structure array
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
KSSV
on 23 Aug 2017
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})
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!