Finding a string exact match

조회 수: 8 (최근 30일)
Pablo De Jarmy
Pablo De Jarmy 2019년 7월 8일
답변: Guillaume 2019년 7월 8일
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.

채택된 답변

Guillaume
Guillaume 2019년 7월 8일
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}, '_']

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by