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
2012 年 11 月 8 日
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
Jan
2012 年 11 月 8 日
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
Thanks Matt!
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
