필터 지우기
필터 지우기

Check if string contains a set of numbers

조회 수: 51 (최근 30일)
Iugo
Iugo 2021년 6월 9일
댓글: Iugo 2021년 6월 9일
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)
Iugo
Iugo 2021년 6월 9일
Thank you @Cris LaPierre, that's it!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by