Extracting numbers from mixed string

I need to extract the four-digit information that is before the period. With these commands that I have, only the zero digit comes out. How can I proceed to obtain the 4 zero digits?
str = 'preci_CZ_02000_20200101_0000.txt';
out = extractBetween(str,25,28);
Thanks!

댓글 수: 2

That's not what I get. I get all four digits.
>> str = 'preci_CZ_02000_20200101_0000.txt';
out = extractBetween(str,25,28)
out =
1×1 cell array
{'0000'}
I'm actually trying to do it inside a loop, because I need to extract from several files.
for x = 1:length(ix20);
namex = files(i).name;
sss = extractBetween(namex,25,28);
hm(x)=string(sss);
end

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

답변 (3개)

Ameer Hamza
Ameer Hamza 2020년 9월 20일
편집: Ameer Hamza 2020년 9월 20일

0 개 추천

Your current code will only work if all the strings have equal length.
For a general case, try using regex
str = 'preci_CZ_02000_20200101_0000.txt';
out_str = regexp(str, '\_([0-9]{4})\.', 'tokens');
out_str = out_str{1}{1};

댓글 수: 4

pink flower
pink flower 2020년 9월 20일
편집: pink flower 2020년 9월 20일
I tried but I couldn't. I'm actually trying to do it inside a loop, because I need to extract from several files.
for x = 1:length(ix20);
str = files(i).name;
out_str = regexp(str, '\_([0-9]{4})\.', 'tokens');
out_str = out_str{1}{1};
hm(x)=string(out_str);
end
And then I need to leave this as a double.
Ameer Hamza
Ameer Hamza 2020년 9월 20일
What is the error? You can use str2num() to convert the values to numeric format.
pink flower
pink flower 2020년 9월 20일
There is no error, but I cannot leave the 4 digits together when there is 0000, 0010, 0020,0030, for example. I don't know what to do anymore!
Ameer Hamza
Ameer Hamza 2020년 9월 20일
I didn't understand the problem. Can you show how do you want to use the output. That will be helpful in suggesting a solution.

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

Star Strider
Star Strider 2020년 9월 20일
편집: Star Strider 2020년 9월 20일

0 개 추천

Another approach:
str = 'preci_CZ_02000_20200101_0000.txt';
out = regexp(str, '(?:\d{4})\>','match');
producing:
out =
1×1 cell array
{'0000'}
EDIT — (20 Sep 2020 at 4:40)
I do not have ‘files.name’, however this approach works and is efficient:
str = compose('preci_CZ_02000_20200101_%04d.txt', 0:5:20).' % Create File Vector
out = regexp(str, '(?:\d{4})\>','match'); % Extract Last Four Digits Before Dot
hm = string([out{:}]).' % Display Results
producing:
str =
5×1 cell array
{'preci_CZ_02000_20200101_0000.txt'}
{'preci_CZ_02000_20200101_0005.txt'}
{'preci_CZ_02000_20200101_0010.txt'}
{'preci_CZ_02000_20200101_0015.txt'}
{'preci_CZ_02000_20200101_0020.txt'}
hm =
5×1 string array
"0000"
"0005"
"0010"
"0015"
"0020"
.
Mario Malic
Mario Malic 2020년 9월 20일
편집: Mario Malic 2020년 9월 20일

0 개 추천

Variable str is actually a char array, you can get last 4 characters before dot by
newstr = str(end-7:end-4)
To get it in double, you can use str2num function.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2020년 9월 20일

댓글:

2020년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by