Finding a string exact match

7 visualizzazioni (ultimi 30 giorni)
Pablo De Jarmy
Pablo De Jarmy il 8 Lug 2019
Risposto: Guillaume il 8 Lug 2019
I have a list of parameters
parameters = {'Freq','I','U','P','Q','S','PF'};
and a list of names
names = {'Freq_min', 'Freq_max, 'Freq_avg','I_min', 'I_max,I_avg'....} and so on. Every parameter has many values like min, max, avg, peak, etc. There are 140 names in the array.
names is a cell array that contains the variable names from table T.
I need to find every position in the array names that has the string 'Freq' and then pull all data rows in that column, so this is the code i'm using:
for n = 1:length(parameters)
x = parameters(~ismember(parameters,parameters{n}));
s.(strcat(parametros{n},'_','Min')) = ...
T(:,contains(names,parameters{n})&contains(nombres,'Min')&strncmpi(parameters(n),names,2));
s.(strcat(parametros{n},'_','Avg')) = ...
T(:,contains(nombres,parameters{n})&contains(names,'Avg')&strncmpi(parameters(n),names,2));
s.(strcat(parametros{n},'_','Max')) = ...
T(:,contains(nombres,parameters{n})&contains(names,'Max')&strncmpi(parameters(n),names,2));
end
And it works well, until it hits 'PF' because it also finds 'P' and i cannot find the way for Matlab to discriminate for 'P' not being containd in 'PF'
Or maybe I'm just over complicating the solution and there's an easier way.
Thanks.

Risposta accettata

Guillaume
Guillaume il 8 Lug 2019
I have no idea what you were trying to do with:
x = parameters(~ismember(parameters,parameters{n}));
which is obtained more simply (and a lot more understandably) with:
x = parameters([1:n-1, n+1:end]);
as long as there's no duplicate entry in parameters
If I understand correctly what you're trying to achieve:
paramlocs = cellfun(@(p) find(StartsWith(names, p)), strcat(parameters, '_'), 'UniformOutput', false)
which will give you a cell array paramlocs where paramlocs{i} is the index of the elements of names which starts with [paramaters{i}, '_']

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by