# The str2double function is taking too long?

10 views (last 30 days)
cui on 10 Jun 2021
Commented: Walter Roberson on 10 Jun 2021
The built-in function "str2double" is very time consuming when I want to convert an array of string type to an array of numertic type, especially when I have an array of string type with many elements(46259*503 size). Is there any way to improve the performance?
my os: win10
matlab 2021a

Walter Roberson on 10 Jun 2021
That gives 46264 rows, 502 variables, everything already numeric.

Walter Roberson on 10 Jun 2021
There have been some test results posted showing that double() of a string() object is even faster than str2double()
format long g
S = compose("%.16g", randn(1000,50));
S(1:3,1:3)
ans = 3×3 string array
"0.6965617957186385" "0.7061472333823291" "-0.7023246730823328" "0.02816411095732173" "1.507324316664719" "-1.236968837728482" "-0.834231469338338" "-0.8834500860277891" "-1.431844364400984"
time_for_double = timeit(@()double(S), 0)
time_for_double =
0.020335829
time_for_str2double = timeit(@()str2double(S), 0)
time_for_str2double =
0.574124829
time_for_sscanf = timeit(@()arrayfun(@(V)sscanf(V, '%f'),S))
time_for_sscanf =
3.451887829
t1 = tic;
arrayfun(@str2double,S);
time_for_str2double = toc(t1)
time_for_str2double =
5.699698
Walter Roberson on 10 Jun 2021
format long g
S = compose("%.16g", randn(1000,50));
S(1:3,1:3)
ans = 3×3 string array
"-0.87290576061866" "0.04846286053980744" "-2.710234520504661" "-1.026614391407934" "-0.0983918203860604" "-0.1155795524871058" "1.340938807862134" "0.2622135116577263" "0.7506333968994494"
t = tic;
double(S);
time_for_double = toc(t)
time_for_double =
0.029172
t0 = tic;
str2double(S);
time_for_str2double = toc(t0)
time_for_str2double =
0.624089
t1 = tic;
arrayfun(@(V) sscanf(V, '%f'),S);
time_for_sscanf = toc(t1)
time_for_sscanf =
3.423017
t2 = tic;
arrayfun(@(V) 1, S);
time_for_arrayfun = toc(t2)
time_for_arrayfun =
2.218439
t3 = tic;
arrayfun(@str2double,S);
time_for_str2double = toc(t3)
time_for_str2double =
5.802806
t4 = tic;
sscanf(sprintf(' %s',S.'), '%f', [size(S,2),Inf]).';
time_for_stephen_sscanf = toc(t4)
time_for_stephen_sscanf =
0.068427
t5 = tic;
reshape(str2num(strjoin(S)),size(S));
time_for_str2num = toc(t5)
time_for_str2num =
0.06454
The last of those is marginally better than your sscanf/sprintf approach... on this run.

R2021a

### Community Treasure Hunt

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

Start Hunting!