Check if string contains a set of numbers

56 views (last 30 days)
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 Comment
Scott MacKenzie
Scott MacKenzie on 9 Jun 2021
Edited: Scott MacKenzie on 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)

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 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 Comments
Cris LaPierre
Cris LaPierre on 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)

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!