Check if string contains a set of numbers

50 次查看(过去 30 天)
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 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-9
编辑:Scott MacKenzie 2021-6-9
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)

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-6-9
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 个评论
Cris LaPierre
Cris LaPierre 2021-6-9
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by