Filter löschen
Filter löschen

Check if string contains a set of numbers

51 Ansichten (letzte 30 Tage)
Iugo
Iugo am 9 Jun. 2021
Kommentiert: Iugo am 9 Jun. 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 Kommentar
Scott MacKenzie
Scott MacKenzie am 9 Jun. 2021
Bearbeitet: Scott MacKenzie am 9 Jun. 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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 9 Jun. 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 Kommentare
Cris LaPierre
Cris LaPierre am 9 Jun. 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 am 9 Jun. 2021
Thank you @Cris LaPierre, that's it!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by