How to read formated data with implied decimal point
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
Is there any option in textscan or sscanf to read numbers with implied decimal format?
For example, in fortran format f4.2 would read 1234 as 12.34
I know that I can do it in two steps with various ways, but is there any way to do it while reading the number?
Thank you Petros
댓글 수: 0
답변 (1개)
Cedric
2013년 4월 16일
편집: Cedric
2013년 4월 16일
Not to my knowledge, but even the conversion in multiple steps is interesting (and it will prop up your question as a small challenge if you are lucky, so you may end up having the answer that you want ;-))..
% Updated @4:50pm EDT.
n = 1e6 ;
s = repmat('1234 5678 9876 5432 1098 ', 1, n) ;
tic ;
s_tmp = s(s >= '0') ;
num_1 = 10.^(1:-1:-2) * reshape(s_tmp-'0', 4, []) ;
toc
tic ;
num_2 = sscanf(s, '%f') / 100 ;
toc ;
tic ;
num_3 = sscanf(s, '%4f') / 100 ;
toc ;
tic ;
num_4 = [1,1/100] * reshape(sscanf(s, '%2f'), 2, []) ;
toc ;
tic ;
num_5 = [10.^(1:-1:-2),0] * reshape(s-'0', 5, []) ;
toc
Output:
Elapsed time is 0.352726 seconds.
Elapsed time is 0.891862 seconds.
Elapsed time is 0.996174 seconds.
Elapsed time is 1.657756 seconds.
Elapsed time is 0.221108 seconds. <= winner so far.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!