# How to read formated data with implied decimal point

10 views (last 30 days)
Petros Bogiatzis on 16 Apr 2013
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

Cedric Wannaz on 16 Apr 2013
Edited: Cedric Wannaz on 16 Apr 2013
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.

### Categories

Find more on Multirate Signal Processing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!