How can i extract the numbers from string?

Im trying to extract the numbers from a cell array? How can i do this?
cell array:
filename =
1×12 cell array
Columns 1 through 4
{'104p8.png'} {'105.png'} {'105p2.png'} {'105p4.png'}
Columns 5 through 8
{'105p6.png'} {'105p8.png'} {'106.png'} {'106p2.png'}
Columns 9 through 12
{'106p4.png'} {'106p6.png'} {'106p8.png'} {'107.png'}
Thanks

댓글 수: 4

Khushboo
Khushboo 2022년 11월 7일
You can refer do it using regular expressions as mentioned here.
What exactly do you expect to be returned from e.g. '104p8.png':
[1,0,4,8]
[1,0,4]
[104,8]
1048
104
or something else... ?
Happy PhD
Happy PhD 2022년 11월 7일
I expect to return the value: 104.8
p is '.'
Marcel
Marcel 2022년 11월 7일
you might wanna update you question with this information

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

 채택된 답변

Stephen23
Stephen23 2022년 11월 7일
편집: Stephen23 2022년 11월 7일
Here are two approaches:
C = {'104p8.png','105.png','105p2.png','105p4.png','105p6.png','105p8.png','106.png','106p2.png','106p4.png','106p6.png','106p8.png','107.png'}
C = 1×12 cell array
{'104p8.png'} {'105.png'} {'105p2.png'} {'105p4.png'} {'105p6.png'} {'105p8.png'} {'106.png'} {'106p2.png'} {'106p4.png'} {'106p6.png'} {'106p8.png'} {'107.png'}
V = str2double(strrep(strrep(C,'.png',''),'p','.'))
V = 1×12
104.8000 105.0000 105.2000 105.4000 105.6000 105.8000 106.0000 106.2000 106.4000 106.6000 106.8000 107.0000
V = str2double(regexprep(C,{'\.png$','p'},{'','.'},'ignorecase')) % more robust
V = 1×12
104.8000 105.0000 105.2000 105.4000 105.6000 105.8000 106.0000 106.2000 106.4000 106.6000 106.8000 107.0000

추가 답변 (1개)

Marcel
Marcel 2022년 11월 7일
편집: Marcel 2022년 11월 7일

0 개 추천

Hi i made a test script and came up with the following code. I found a solution here
example = cellstr(["1024.png", "image1003.png", "photo-1234.png"])
for i=1:length(example)
name = example{i};
numbers = str2double(extract(name, digitsPattern))
end
example =
1×3 cell array
{'1024.png'} {'image1003.png'} {'photo-1234.png'}
numbers =
1024
numbers =
1003
numbers =
1234

카테고리

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

제품

릴리스

R2021b

태그

질문:

2022년 11월 7일

편집:

2022년 11월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by