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 Comments

You forgot to explain what you want as output.
>> strrep(S,' ','')
ans =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '

Sign in to comment.

 Accepted Answer

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 Comments

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?
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...
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
WHat is the output of strrep(S,' ',''))?
>> strrep(S,' ','')
ans =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 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....
>> 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!
O.k., something funny is going on. Please print the output of this code:
S=regexp(cline,'\t','split');
double(S{1})
>> 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
Aha! They are not spaces. That is the problem. So try,
S=regexp(cline,'\t','split');
str2double(strrep(S,char(0),''))
>> S=regexp(cline,'\t','split'); str2double(strrep(S,char(0),''))
ans =
170.9201 562.3333
Sucesss
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.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!