finding a string variable in structure array

3 visualizaciones (últimos 30 días)
Trevor
Trevor el 23 de Ag. de 2017
Comentada: Trevor el 30 de Ag. de 2017
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.

Respuestas (1)

KSSV
KSSV el 23 de Ag. de 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})
  1 comentario
Trevor
Trevor el 30 de Ag. de 2017

Hi KSSV,

Thanks for your answer. However, it was not quite what I was looking for, but more like:

name = struct() ;
name(1).fnme(1).nm = 'Tom';
name(2).fnme(1).nm = 'Dick';
name(3).fnme(1).nm = 'Harry'; 
%% Get Dick from the structure 
for n=1:length(name)
    names = fieldnames(string(name(n).fnme(1).nm{1,1})) ;
    names1 = string(names);
    idx = find(not(cellfun('contains', strfind(names1,'Dick'))));
    iwant = getfield(name(n).fnme.nm,names1{idx});
end 

However, it was solved by:

    nSeasons = 4;
    NAME=1; SEASON=2;
    load('datafile.mat');
    Where Nm is a cellArray with {station-name and season}
    stationname = {'Tom','Dick','Harry'}
    season = {'wint', 'sprn', 'autm', 'summ'}
    for ci = 1:length(datafile)
        for si = 1:nSeasons
            name_season_pair = datafile(ci).Cson(si).Nm;
            name_in_data = name_season_pair{NAME};
            season_in_data = name_season_pair{SEASON};
            if strcmp(name_in_data, stationname) && strcmp(season_in_data, season)
                location_ind = ci;
                season_ind = si;
                break;
            end
        end
    end
% Set-up return
S  = datafile(location_ind).Cson(season_ind);

season was not necessary but proved useful.

I am sorry if my request was misleading or incomplete but thanks for your help as it gave the basis for the solution.

Regards T

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by