function results = strs2number(cellStrs)
numVals = numel(cellStrs);
results = cellStrs;
powersOf10 = [10000000000000 1000000000000 100000000000 10000000000 ...
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1];
Nmax = 14;
for idx = 1 : numVals
value = cellStrs{idx};
N = length(value);
if N==0
results{idx} = [];
continue
elseif value(1) > '9'
continue
elseif N > Nmax
continue
end
isDigit = value>='0' & value<='9';
if all(isDigit)
powers = powersOf10(Nmax-N+1:Nmax);
results{idx} = sum((value-'0') .* powers);
else
isDot = value=='.';
if all(isDigit | isDot)
dotIdx = find(isDot);
if numel(dotIdx) > 1, continue, end
N = N - 1;
shift = dotIdx - N - 1;
factor = 10^shift;
n1 = Nmax-N; n2 = n1+dotIdx;
powers = [powersOf10(n1+1:n2-1), 0, powersOf10(n2:Nmax)];
numericVal = sum((value-'0') .* powers) * factor;
results{idx} = numericVal;
elseif any(value==' ') || any(value=='/') || any(value>'9') || sum(value=='-') > 1 || sum(isDot) > 1
else
[numericVal,count,errMsg,nextIndex] = sscanf(value,'%f',1);
if count == 1 && isempty(errMsg) && nextIndex > N
results{idx} = numericVal;
else
end
end
end
end
end
2 Comments
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/724348-can-i-somehow-improve-performance-of-str2double#comment_1279243
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/724348-can-i-somehow-improve-performance-of-str2double#comment_1279243
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/724348-can-i-somehow-improve-performance-of-str2double#comment_1279848
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/724348-can-i-somehow-improve-performance-of-str2double#comment_1279848
Sign in to comment.