필터 지우기
필터 지우기

Extract Numbers from a String

조회 수: 1 (최근 30일)
Matthew Brandsema
Matthew Brandsema 2014년 11월 23일
편집: Matthew Brandsema 2014년 11월 23일
I have the following string from an excel file.
f 27//1 29//2 5//3 2//4
I want to extract the first number before each // sign. So for example, in this case, I need to extract the numbers 27 29 5 2
I am looking up how to do but but cannot seem to find a straightforward way, would anyone be able to clear things up for me? Thanks!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
a='f 27//1 29//2 5//3 2//4'
b=str2double(regexp(a,'\S+(?=//)','match'))
  댓글 수: 2
Matthew Brandsema
Matthew Brandsema 2014년 11월 23일
Thanks a lot! This is exactly what I need!
Matthew Brandsema
Matthew Brandsema 2014년 11월 23일
편집: Matthew Brandsema 2014년 11월 23일
Okay I realized upon importing the xls file, I actually get the form
'27//1'
for the cells in question. I am messing around with regexp but I simply do not have enough experience with it to know what I am doing. How would I extract the first number in cells that look like that? I tried using the same type of thing but it isn't working.
The furthest I got was get '27' But the ' symbols are messing up the str2num function.

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

추가 답변 (1개)

the cyclist
the cyclist 2014년 11월 23일
편집: the cyclist 2014년 11월 23일
You want the regexp command. For example,
s = 'f 27//1 29//2 5//3 2//4'
regexp(s,'//')
ans =
5 11 16 21
finds the positions of the //.
You could similarly find the positions of the spaces, and the numbers you want are between them.
You may then need to use str2num (or similar command) to convert the strings to numeric variables.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by