Finding number in a string

Dear all and thanks for your help, I have a Cell array (D) in which each cell is composed of string and characters like AB878546HD. In anoter cell (C) I have a list of numbers and I want to find the location of each number in cell C in cell D.
I have tried to use the strfind function k = strfind(D,C{2}) but it didn’t work - I didnt get an errore but got ans=[] .
If instead of C{2} I write k = strfind(D,'38306') it works and the wanted cell was founded. So I dont know what is wrong Any help will be appreciated, here is example parts of C
' 36462' ' 38306' ' 3113' ' 1305' ' 22608' ' 11086' ' 7541'
and D
'jgi|Thaps3|40|fgenesh1_kg.C_chr_4000008'
'jgi|Thaps3|36462|fgenesh1_kg.C_chr_4000014'
'jgi|Thaps3|3113|fgenesh1_kg.C_chr_5000009'
'jgi|Thaps3|93|fgenesh1_kg.C_chr_8000006'
'jgi|Thaps3|105|fgenesh1_kg.C_chr_9000002'
'jgi|Thaps3|168|fgenesh1_pm.C_chr_1000013'
Shilo

5 Commenti

Friedrich
Friedrich il 7 Mar 2012
Actually strfind(D,C{2}) should work when the number in C are also strings. If not use strfind(D,num2str(C{2})).
Can you give us an example how D and C are actually looking?
Shilo
Shilo il 7 Mar 2012
Thanks Friedrich, but it didnt work also. here is example parts of C
' 36462'
' 38306'
' 3113'
' 1305'
' 22608'
' 11086'
' 7541'
and D 'jgi|Thaps3|40|fgenesh1_kg.C_chr_4000008'
'jgi|Thaps3|46|fgenesh1_kg.C_chr_4000014'
'jgi|Thaps3|55|fgenesh1_kg.C_chr_5000009'
'jgi|Thaps3|93|fgenesh1_kg.C_chr_8000006'
'jgi|Thaps3|105|fgenesh1_kg.C_chr_9000002'
'jgi|Thaps3|168|fgenesh1_pm.C_chr_1000013'
If instead of C{2} I write k = strfind(D,'38306') it works. So I dont know what is wrong
Friedrich
Friedrich il 7 Mar 2012
What exactly is not working? Do you get an error? If so, what does the error say?
Please edit your question to contain the complete example.
Shilo
Shilo il 7 Mar 2012
Hi Friedrich,
I didnt get and error but when I use find(~cellfun('isempty', k));
I got ans =[]
Thanks

Accedi per commentare.

 Risposta accettata

Ken Atwell
Ken Atwell il 8 Mar 2012
The following will create a numeric array, N, where the value of N(x) indicates which element of D contained the string C(x). If no match was found, N(x) is NaN. The text search includes the '|' delimiter to avoid cases like "93" matching not only "93", but also "930", "931", "193", "293", etc.
D= {'jgi|Thaps3|40|fgenesh1_kg.C_chr_4000008' ...
'jgi|Thaps3|36462|fgenesh1_kg.C_chr_4000014' ...
'jgi|Thaps3|3113|fgenesh1_kg.C_chr_5000009' ...
'jgi|Thaps3|93|fgenesh1_kg.C_chr_8000006' ...
'jgi|Thaps3|105|fgenesh1_kg.C_chr_9000002' ...
'jgi|Thaps3|168|fgenesh1_pm.C_chr_1000013' };
C = {' 36462' ' 38306' ' 3113' ' 1305' '22608' ' 11086' ' 7541'};
C = strtrim(C); % Trim whitespace from C
N = nan(size(C));
for i = 1:numel(C);
found = find(cellfun(@(x) ~isempty(x), strfind(D, ['|' C{i} '|'] )));
if ~isempty(found)
N(i) = found;
end
end
N
An exceptionally clever person might determine a way to do this without a for loop, but it would likely sacrifice readability.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by