how to read numbers in a string where white-space separated everthing
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Jan
on 8 Nov 2012
You forgot to explain what you want as output.
Francois Doucet
on 8 Nov 2012
>> strrep(S,' ','')
ans =
' 3 9 8 . 6 2 0 0 0 0 ' ' 1 3 0 2 7 1 . 9 9 1 6 0 8 '
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
Francois Doucet
on 8 Nov 2012
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...
Francois Doucet
on 8 Nov 2012
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
on 8 Nov 2012
WHat is the output of strrep(S,' ',''))?
Francois Doucet
on 8 Nov 2012
>> 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....
Francois Doucet
on 8 Nov 2012
>> 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
on 8 Nov 2012
O.k., something funny is going on. Please print the output of this code:
S=regexp(cline,'\t','split');
double(S{1})
Francois Doucet
on 8 Nov 2012
>> 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
on 8 Nov 2012
Aha! They are not spaces. That is the problem. So try,
S=regexp(cline,'\t','split');
str2double(strrep(S,char(0),''))
Francois Doucet
on 8 Nov 2012
>> S=regexp(cline,'\t','split'); str2double(strrep(S,char(0),''))
ans =
170.9201 562.3333
Sucesss
Francois Doucet
on 8 Nov 2012
Thanks Matt!
Matt Fig
on 8 Nov 2012
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
on 8 Nov 2012
big-endian/small-endian?
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Tags
See Also
on 8 Nov 2012
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)