Check if string contains a set of numbers

68 vues (au cours des 30 derniers jours)
Iugo
Iugo le 9 Juin 2021
Commenté : Iugo le 9 Juin 2021
Hello everyone!
I have a cell with several names like this example "'NeuroIMAGEsfnwmrda1017176session1rest1aalTCs.mat'" and a variable with several numbers like "2371032" and so on.
I want to know if it is possible to check if a name in that respective cell contains a number that is present in another variable or the opposite, and if so, how can I achieve that?
Thanks in advance!
  1 commentaire
Scott MacKenzie
Scott MacKenzie le 9 Juin 2021
Modifié(e) : Scott MacKenzie le 9 Juin 2021
I used strfind with good results:
C = {'NeuroIMAGEsfnwmrda1017176session1rest1aalTCs.mat'};
var1 = '176';
var2 = '789';
strfind(C{:}, var1) % output = 23 (found at index 23)
strfind(C{:}, var2) % output = [] (not found)

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 9 Juin 2021
It would be helpful to have more data to test with, but my approach would probably be to convert the cell of names to a string array, and then use digitspattern to extract the 7-digit number. I could then convert the output of digitspattern to numbers, and then use ismember to see which of those numbers exist in your variable of numbers.
C = {'NeuroIMAGEsfnwmrda1017176session1rest1aalTCs.mat'};
n = [2371032;1017176];
pat = digitsPattern(7);
nums = extract(string(C),pat)
nums = "1017176"
[Lia,locb] = ismember(str2double(nums),n)
Lia = logical
1
locb = 2
  9 commentaires
Cris LaPierre
Cris LaPierre le 9 Juin 2021
Ah, well in that case, digitsPattern did not yet exist. You could try using regexp.
Here's an example that works for the minimal data you provided. It at least gives you a starting point.
C = {'NeuroIMAGEsfnwmrda1017176session1rest1aalTCs.mat'};
n = [2371032;1017176];
nums = regexp(C,'\d{7}','match')
[Lia,locb] = ismember(str2double(nums{:}),n)
Iugo
Iugo le 9 Juin 2021
Thank you @Cris LaPierre, that's it!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by