Splitting a string of decimal point numbers
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
2014년 2월 25일
@Jos: Quite cool solution, however how are you doing with numbers as 1.34? Are they allowed?
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
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
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 Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!