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 个评论
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 '
采纳的回答
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 个评论
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.
big-endian/small-endian?
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
产品
标签
另请参阅
2012-11-8
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
