how to read numbers in a string where white-space separated everthing

how to read numbers in a string where white-space separated everything here an example for 2 numbers in a string tab separated: '3 9 8 . 6 1 9 5 0 0 1 3 0 3 1 3 . 2 1 2 7 6 0'
Thanks, Frank

2 件のコメント

Jan
Jan 2012 年 11 月 8 日
You forgot to explain what you want as output.
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
>> strrep(S,' ','')
ans =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '

サインインしてコメントする。

 採用された回答

Matt Fig
Matt Fig 2012 年 11 月 8 日
編集済み: Matt Fig 2012 年 11 月 8 日

1 投票

This is not possible without more information. Say you do this:
S=strrep('3 9 8 . 6 1 9 5 0 0 1 3 0 3 1 3 . 2 1 2 7 6 0',' ','')
% S='398.619500130313.212760'
Should the second number be 313.212760 or 130313.212760 or 3.212760 or any of the other several or so combinations? You need to provide more information to be able to decipher the numbers.

14 件のコメント

Francois Doucet
Francois Doucet 2012 年 11 月 8 日
My string is this one: ' 3 9 8 . 6 2 0 0 0 0 1 3 0 2 7 1 . 9 9 1 6 0 8 ' 1x49 char there is a tab between the two numbers (i.e. 398.620000 and 130271.991608) I tried [a , b] = strread(' 3 9 8 . 6 2 0 0 0 0 1 3 0 2 7 1 . 9 9 1 6 0 8 ','%d/t%d') but it does not work. have any clue?
Matt Fig
Matt Fig 2012 年 11 月 8 日
編集済み: Matt Fig 2012 年 11 月 8 日
Yes, use REGEXP to split on the tab, then call STR2DOUBLE on the return from STRREP.
% First make the string as you say it is.
S = sprintf('%s\t%s','4 5 . 5 6','3 1 . 2 1');
% Now extract the numbers.
S = regexp(S,'\t','split');
str2double(strrep(S,' ','')) % This could be one line...
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
S=regexp(cline,'\t','split')
S =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '
>> str2double(strrep(S,' ',''))
ans =
NaN NaN
Hum NaN
Jan
Jan 2012 年 11 月 8 日
WHat is the output of strrep(S,' ',''))?
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
>> strrep(S,' ','')
ans =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '
Matt Fig
Matt Fig 2012 年 11 月 8 日
編集済み: Matt Fig 2012 年 11 月 8 日
Francois, there may be a difference in how the webpage is displaying things. When I look at your output from
strrep(S,' ','')
it looks identical to S itself. Is that the case? In other words, is this true:
S=regexp(cline,'\t','split');
isequal(S,strrep(S,' ','')) % 1 or 0???
When I simply copy and paste the S you show as the output from above, and then call STRREP, I get this:
% Here I copied from the Francois post above,
% adding the braces to make a cell array as shown...
S = {' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '};
strrep(S,' ','')
ans =
'398.620000' '130271.991608'
No spaces left whatsoever....
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
>> S=regexp(cline,'\t','split'); isequal(S,strrep(S,' ',''))
ans =
1
It appears it is. However, when I do a isspace on the non separated string (cline) I got this: >> cline
cline =
1 7 0 . 9 2 0 1 0 0 5 6 2 . 3 3 3 3 3 3
>> isspace(cline)
ans =
Columns 1 through 18
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 19 through 36
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 37 through 43
0 0 0 0 0 0 0
It detect only 1 space, and it appears that there is more than one!
Matt Fig
Matt Fig 2012 年 11 月 8 日
O.k., something funny is going on. Please print the output of this code:
S=regexp(cline,'\t','split');
double(S{1})
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
>> S=regexp(cline,'\t','split'); double(S{1})
ans =
Columns 1 through 18
0 49 0 55 0 48 0 46 0 57 0 50 0 48 0 49 0 48
Columns 19 through 21
0 48 0
Matt Fig
Matt Fig 2012 年 11 月 8 日
Aha! They are not spaces. That is the problem. So try,
S=regexp(cline,'\t','split');
str2double(strrep(S,char(0),''))
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
>> S=regexp(cline,'\t','split'); str2double(strrep(S,char(0),''))
ans =
170.9201 562.3333
Sucesss
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
Thanks Matt!
Matt Fig
Matt Fig 2012 年 11 月 8 日
Glad to help! I am still curious as to how you ended up with char(0) instead of char(32), but I guess we may never know.
Francois Doucet
Francois Doucet 2012 年 11 月 8 日
big-endian/small-endian?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by