필터 지우기
필터 지우기

How to convert a string of characters to numbers?

조회 수: 1 (최근 30일)
carlos Uribe
carlos Uribe 2012년 9월 28일
I have the following string obtained from a text file (the text file has a lot of info so I had to go and look for certain lines):
0 0 0 0 0.1 0 0 0 BlaBla
Another line might look like this
1 0.013 0.0156 -0.104 0.0376 0.0624 0.132 0.132 BlaBlaBla
By the way I'm reading the file everything is now a string of characters.
How can I convert that to a vector? Notice that the last column is a string of characters. The white spaces on the first line is different than on the second one too.
Any help is appreciated thanks.

채택된 답변

Matt Fig
Matt Fig 2012년 9월 28일
편집: Matt Fig 2012년 9월 28일
str = '1 0.013 0.0156 -0.104 0.0376 0.0624 0.132 0.132 Bla';
T = regexp(str,'-*\d*\.*\d*','match');
V = cellfun(@str2double,T)
  댓글 수: 7
carlos Uribe
carlos Uribe 2012년 9월 28일
My bad..didn't see that you added two new values to cellfun...thanks for your help
Matt Fig
Matt Fig 2012년 9월 28일
Jan, that returns nans here...

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

추가 답변 (1개)

Jan
Jan 2012년 9월 28일
str = '1 0.013 0.0156 -0.104 0.0376 0.0624 0.132 0.132 Bla';
V = sscanf(str, '%g', 6);
  댓글 수: 1
Matt Fig
Matt Fig 2012년 9월 28일
Or for cell str:
V = cellfun(@(x) sscanf(x, '%g'),str,'Un',0);
Very nice... no call to REGEXP.

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

카테고리

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