필터 지우기
필터 지우기

Extract the number from the file name

조회 수: 3 (최근 30일)
Songman Liao
Songman Liao 2018년 1월 25일
답변: Walter Roberson 2018년 1월 25일
Hi, I have 24 files in the folder. 12 of them have names as: "AMS1701-AMsent1.log", "AMS1701-AMsent2.log" to "AMS1701-AMsent12.log". And the rest 12 files have names as: "AMS1701.02-AMsent1.log", "AMS1701.02-AMsent2.log" till "AMS1701.02-AMsent12.log". I want to extract 2 type of number. The first type is the number after AMS1701(whether there is a 02 or not). The second type of number is the one after"AMsent". It can vary from 1 to 12.
Thank you so much for helping me! I would really appreciate if you could show me the procedure.

답변 (2개)

the cyclist
the cyclist 2018년 1월 25일
Two commands that are going to be helpful are
  • what -- List MATLAB files in folder
  • regexp -- Match regular expressions
The latter command will allow you to pattern-match for the expressions relevant to what you are searching for. It can be tricky to get used to, so maybe play around a little and then ask for more specific help if you need it.
Or maybe someone here will be a little less lazy than I am being right now, and write out the full solution for you. :-)

Walter Roberson
Walter Roberson 2018년 1월 25일
To detect the .02:
has_02 = isempty( strfind(filename, '.02') å);
You are not clears as to what number you want to extract. I suspect you want the number before the '.log' part:
out = regexp(filename, '\d+(?=\.log)', 'match', 'once')
you could str2double() that if you wanted.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by