Splitting a string of decimal point numbers

8 ビュー (過去 30 日間)
Ajay
Ajay 2014 年 2 月 25 日
コメント済み: Jos (10584) 2014 年 2 月 25 日
I have a string which looks like this 500.051000.353000.3510000.2015000.05 I want to split this string into a cell array with each row containing numbers which look like this.
50 0.05 100 0.35 300 0.35 1000 0.20 1500 0.05
How can I achieve this.

採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 25 日
Apparently the format of two numbers is as follows
x0.yyx0.yy
where x stands for any number of digits and yy are exactly two digits. There are no spaces or other separating characters.
You can try this:
str = '10.22330.4450.667770.88' % example
str2 = strrep(str,'0.',' 0.') % add spaces
v = sscanf(str2, '%f 0.%2f') % read it
v(2:2:end) = v(2:2:end)/100 % convert every 2nd value to 0.yy
v =v.' % make it a row vector
  3 件のコメント
Patrik Ek
Patrik Ek 2014 年 2 月 25 日
@Jos: Quite cool solution, however how are you doing with numbers as 1.34? Are they allowed?
Jos (10584)
Jos (10584) 2014 年 2 月 25 日
@Patrik, thanks. No, as per the example of the OP it is assumed that there as always a zero before the decimal dot. If not, you have to change the engine. It actually can become a one-liner then ;-)
str = '19.22334.4450.667773.88'
v = sscanf(regexprep(str,'\d\.\d\d',' $0 '), '%f').'

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

その他の回答 (1 件)

Iain
Iain 2014 年 2 月 25 日
You need to apply knowledge of what you've got.
You might have, if you inspect the string closely, a separating, non-printing character between the 50 and 0.05, which you can use to separate all your numbers
Alternatively, if there is no separating character, you'll need to apply knowledge, something like every 2nd number follows the form "x.xx".
  2 件のコメント
Ajay
Ajay 2014 年 2 月 25 日
the problem is there is no character in between 50 and 0.05 and alternatively I was kind of looking for some kind of regexp to split this expression...
Patrik Ek
Patrik Ek 2014 年 2 月 25 日
編集済み: Patrik Ek 2014 年 2 月 25 日
Well, then there is a problem. If you have no knowledge of either format or space between the numbers, a part of the string could be anything. Take for example the first 5 characters. They could be [5 00.05] or [50 0.05] or [500.0 5]. All of them equally correct unless stated differently. May I ask how the numbers are acquired? I mean if anything could be changed to make the array of values readable? I can add that knowledge of the format is used in almost all computers. A stream of numbers are usually split into equal length subsets. Eg a stream of 8 bit binarys:
001101001011100100000110
can be split like
00110100 10111001 00000110.
Notice that the stream does not say value the string different but the controller (or what so ever) know that each symbol is an 8 bit binary and should then contain 8 characters (or what they are called in that particular device) per symbol, filled upp with zeros if the symbol is smaller than 128. also the system move operation in windows require the filenames to be separated with space since the filenames can have different length. Windows sorting function require a format like hi0001, hi0002, hi0003, to properly sort files. The list can be large, but what all have in common is that the format need to be specified in every case. However if it is possible to recreate the data I can help you to find a good way to separate the numbers and read them.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by