Search/Find sub strings in a string
Afficher commentaires plus anciens
Hello,
I have two cell arrays:
- The first cell array is a 1x279 cell array which contains components abbreviations like - Ra, Bt, Rag, Rg, Vm, SzF and so on.
- The second cell array is a 1x439 cell array which contains strings like 'F.y_1T.Rg1_N7' or 'F.y_1T.Ra1.SzF2_N590'
You can see the component abbreviations of the first cell array are in the second cell arrays as a substring / part of the whole string .
- For example second cell array 'F.y_1T.Rg1_N7' -> Rg -component abbreviation found from the first cell array. (The rest of the string is something else - not important).
- Second Example: Second cell array F.y_1T.Ra1.SzF2_N590 -> Ra and SzF are found from the the first cell array
So my target is to know which of the 279 component abbreviations are found in the second array? I tried a lot but with no working solution. I forgot my matlab skills cause i dont need very often.
Here is a small part of my code (not working/ not the whole code)
for t=1:size;%279
for h=1:numCols1%439
locigal(t,h) = strcmp(Messstelle{1,h}, abbrev_components{1,t})
%locigal_1{t,h} = strcmp(Messstelle{1,h}, abbrev_components{1,t})
%locigal{t,h} = strmatch(Messstelle{1,h},abbrev_components{1,t})
end
end
What is the difference between 'F.y_1T.Rg1_N7'`and "F.y_1T.Rg1_N7" both are cell arrays. How can i convert it?
Thank you
2 commentaires
"What is the difference between 'F.y_1T.Rg1_N7'`and "F.y_1T.Rg1_N7" both are cell arrays."
- 'F.y_1T.Rg1_N7' is a character vector.
- "F.y_1T.Rg1_N7" is a string scalar.
If the text contains Rag, do you want to prevent matching Ra ? If so, how?
Philipp Mueller
le 9 Fév 2021
Réponses (2)
This matches abbreviations followed by one digit (thus avoiding the Ra/Rag matching problem):
C = {'Ra','Bt','Rag','Rg','Vm','SzF'};
D = {'F.y_1T.Rg1_N7','F.y_1T.Rag1.SzF2_N590'}; % Ra changed to Rag !
rgx = sprintf('|%s',C{:});
rgx = sprintf('(%s)%s',rgx(2:end),'(?=\d)');
tmp = regexp(D,rgx,'match');
tmp{:}
If there can be other characters trailing the abbreviations then adapt the lookahead assertion as required:
After this you can simply do ismember on each cell of tmp:
boo = cellfun(@(c)ismember(C,c),tmp,'uni',0);
boo = vertcat(boo{:})
2 commentaires
Philipp Mueller
le 12 Fév 2021
"I just want to have a simple array where are no duplicate entries."
Each row of array boo contains true to indicate if an abbreviation occurs one or more times in the corresponding string. It does not contain duplicate true values for any one abbreviation.
Please give an example string with duplicate values, and also the expected output.
KeyList = {'Ra', 'Bt', 'Rag', 'Rg', 'Vm', 'SzF'};
StringList = {'F.y_1T.Rg1_N7', 'F.y_1T.Ra1.SzF2_N590', ...
'F.y_1T.Rag.SzF2_N590'};
nKey = numel(KeyList);
nString = numel(StringList);
L = false(nString, nKey);
for t = 1:nKey
% Mask other keys:
exclude = find(contains(KeyList, KeyList{t}));
exclude(exclude == t) = [];
S = StringList;
for k = exclude
S = strrep(S, KeyList{k}, '*');
end
L(:, t) = contains(S, KeyList{t});
% Matlab < R2016b:
% L(:, t) = ~strcmp(S, strrep(StringList, KeyList{t}, ''));
end
Now "Rag" is masked, if "Ra" is searched.
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!