1function [num,status,strarray] = biosig_str2double(s,cdelim,rdelim,ddelim) 2%% STR2DOUBLE converts strings into numeric values 3%% [NUM, STATUS,STRARRAY] = STR2DOUBLE(STR) 4%% 5%% Because of a name space conflict, this function is renamed to 6%% BIOSIG_STR2DOUBLE(...). An alternative is STR2ARRAY from the NaN-toolbox. 7%% 8%% STR2DOUBLE can replace STR2NUM, but avoids the insecure use of EVAL 9%% on unknown data [1]. 10%% 11%% STR can be the form '[+-]d[.]dd[[eE][+-]ddd]' 12%% d can be any of digit from 0 to 9, [] indicate optional elements 13%% NUM is the corresponding numeric value. 14%% if the conversion fails, status is -1 and NUM is NaN. 15%% STATUS = 0: conversion was successful 16%% STATUS = -1: couldnot convert string into numeric value 17%% STRARRAY is a cell array of strings. 18%% 19%% Elements which are not defined or not valid return NaN and 20%% the STATUS becomes -1 21%% STR can be also a character array or a cell array of strings. 22%% Then, NUM and STATUS return matrices of appropriate size. 23%% 24%% STR can also contain multiple elements. 25%% default row-delimiters are: 26%% NEWLINE, CARRIAGE RETURN and SEMICOLON i.e. ASCII 10, 13 and 59. 27%% default column-delimiters are: 28%% TAB, SPACE and COMMA i.e. ASCII 9, 32, and 44. 29%% default decimal delimiter is '.' char(46), sometimes (e.g in 30%% Tab-delimited text files generated by Excel export in Europe) 31%% might used ',' as decimal delimiter. 32%% 33%% [NUM, STATUS] = STR2DOUBLE(STR,CDELIM,RDELIM,DDELIM) 34%% CDELIM .. [OPTIONAL] user-specified column delimiter 35%% RDELIM .. [OPTIONAL] user-specified row delimiter 36%% DDELIM .. [OPTIONAL] user-specified decimal delimiter 37%% CDELIM, RDELIM and DDELIM must contain only 38%% NULL, NEWLINE, CARRIAGE RETURN, SEMICOLON, COLON, SLASH, TAB, SPACE, COMMA, or ()[]{} 39%% i.e. ASCII 0,9,10,11,12,13,14,32,33,34,40,41,44,47,58,59,91,93,123,124,125 40%% 41%% Examples: 42%% str2double('-.1e-5') 43%% ans = -1.0000e-006 44%% 45%% str2double('.314e1, 44.44e-1, .7; -1e+1') 46%% ans = 47%% 3.1400 4.4440 0.7000 48%% -10.0000 NaN NaN 49%% 50%% line ='200,300,400,NaN,-inf,cd,yes,no,999,maybe,NaN'; 51%% [x,status]=str2double(line) 52%% x = 53%% 200 300 400 NaN -Inf NaN NaN NaN 999 NaN NaN 54%% status = 55%% 0 0 0 0 0 -1 -1 -1 0 -1 0 56%% 57%% Reference(s): 58%% [1] David A. Wheeler, Secure Programming for Linux and Unix HOWTO. 59%% http://en.tldp.org/HOWTO/Secure-Programs-HOWTO/ 60 61%% This program is free software; you can redistribute it and/or 62%% modify it under the terms of the GNU General Public License 63%% as published by the Free Software Foundation; either version 2 64%% of the License, or (at your option) any later version. 65%% 66%% This program is distributed in the hope that it will be useful, 67%% but WITHOUT ANY WARRANTY; without even the implied warranty of 68%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 69%% GNU General Public License for more details. 70%% 71%% You should have received a copy of the GNU General Public License 72%% along with this program; if not, write to the Free Software 73%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 74 75%% Copyright (C) 2004,2008,2020 by Alois Schloegl <alois.schloegl@gmail.com> 76%% This function is part of Biosig https://biosig.sourceforge.io 77 78FLAG_OCTAVE = exist('OCTAVE_VERSION','builtin'); 79VER = version; 80 81% valid_char = '0123456789eE+-.nNaAiIfF'; % digits, sign, exponent,NaN,Inf 82valid_delim = char(sort([0,9:14,32:34,abs('()[]{},;:"|/')])); % valid delimiter 83if nargin < 1, 84 error('missing input argument.') 85end; 86if nargin < 2, 87 cdelim = char([9,32,abs(',')]); % column delimiter 88else 89 % make unique cdelim 90 cdelim = char(sort(cdelim(:))); 91 tmp = [1;1+find(diff(abs(cdelim))>0)]; 92 cdelim = cdelim(tmp)'; 93end; 94if nargin < 3, 95 rdelim = char([0,10,13,abs(';')]); % row delimiter 96else 97 % make unique rdelim 98 rdelim = char(sort(rdelim(:))); 99 tmp = [1;1+find(diff(abs(rdelim))>0)]; 100 rdelim = rdelim(tmp)'; 101end; 102if nargin<4, 103 ddelim = '.'; 104elseif length(ddelim)~=1, 105 error('decimal delimiter must be exactly one character.'); 106end; 107 108% check if RDELIM and CDELIM are distinct 109delim = sort(abs([cdelim,rdelim,ddelim])); 110tmp = [1; 1 + find(diff(delim')>0)]'; 111delim = delim(tmp); 112%[length(delim),length(cdelim),length(rdelim)] 113if length(delim) < (length(cdelim)+length(rdelim))+1, % length(ddelim) must be one. 114 error('row, column and decimal delimiter are not distinct.'); 115end; 116 117% check if delimiters are valid 118tmp = sort(abs([cdelim,rdelim])); 119flag = zeros(size(tmp)); 120k1 = 1; 121k2 = 1; 122while (k1 <= length(tmp)) && (k2 <= length(valid_delim)), 123 if tmp(k1) == valid_delim(k2), 124 flag(k1) = 1; 125 k1 = k1 + 1; 126 elseif tmp(k1) < valid_delim(k2), 127 k1 = k1 + 1; 128 elseif tmp(k1) > valid_delim(k2), 129 k2 = k2 + 1; 130 end; 131end; 132if ~all(flag), 133 error('Invalid delimiters!'); 134end; 135 136%%%%% various input parameters 137if isnumeric(s) 138 if all(s<256) && all(s>=0) 139 s = char(s); 140 else 141 error('STR2DOUBLE: input variable must be a string') 142 end; 143end; 144 145num = []; 146status = 0; 147strarray = {}; 148if isempty(s), 149 return; 150 151elseif iscell(s), 152 strarray = s; 153 154elseif ischar(s) 155if all(size(s)>1), %% char array transformed into a string. 156 for k = 1:size(s,1), 157 tmp = find(~isspace(s(k,:))); 158 strarray{k,1} = s(k,min(tmp):max(tmp)); 159 end; 160 161else %if isschar(s), 162 num = []; 163 status = 0; 164 strarray = {}; 165 166 s(end+1) = rdelim(1); % add stop sign; makes sure last digit is not skipped 167 168 RD = zeros(size(s)); 169 for k = 1:length(rdelim), 170 RD = RD | (s==rdelim(k)); 171 end; 172 CD = RD; 173 for k = 1:length(cdelim), 174 CD = CD | (s==cdelim(k)); 175 end; 176 177 k1 = 1; % current row 178 k2 = 0; % current column 179 k3 = 0; % current element 180 181 sl = length(s); 182 ix = 1; 183 %while (ix < sl) & any(abs(s(ix))==[rdelim,cdelim]), 184 while (ix < sl) && CD(ix), 185 ix = ix + 1; 186 end; 187 ta = ix; te = []; 188 while ix <= sl; 189 if (ix == sl), 190 te = sl; 191 end; 192 %if any(abs(s(ix))==[cdelim(1),rdelim(1)]), 193 if CD(ix), 194 te = ix - 1; 195 end; 196 if ~isempty(te), 197 k2 = k2 + 1; 198 k3 = k3 + 1; 199 if te<ta, 200 strarray{k1,k2} = []; 201 else 202 strarray{k1,k2} = s(ta:te); 203 end; 204 %strarray{k1,k2} = [ta,te]; 205 206 flag = 0; 207 %while any(abs(s(ix))==[cdelim(1),rdelim(1)]) & (ix < sl), 208 while CD(ix) && (ix < sl), 209 flag = flag | RD(ix); 210 ix = ix + 1; 211 end; 212 213 if flag, 214 k2 = 0; 215 k1 = k1 + 1; 216 end; 217 ta = ix; 218 te = []; 219 end; 220 ix = ix + 1; 221 end; 222end; 223else 224 error('STR2DOUBLE: invalid input argument'); 225end; 226 227[nr,nc]= size(strarray); 228status = zeros(nr,nc); 229num = repmat(NaN,nr,nc); 230 231for k1 = 1:nr, 232for k2 = 1:nc, 233 t = strarray{k1,k2}; 234 if (length(t)==0), 235 status(k1,k2) = -1; %% return error code 236 num(k1,k2) = NaN; 237 else 238 %% get mantisse 239 g = 0; 240 v = 1; 241 if t(1)=='-', 242 v = -1; 243 l = min(2,length(t)); 244 elseif t(1)=='+', 245 l = min(2,length(t)); 246 else 247 l = 1; 248 end; 249 250 if strcmp(lower(t(l:end)),'inf') 251 num(k1,k2) = v*inf; 252 253 elseif strcmp(lower(t(l:end)),'nan'); 254 num(k1,k2) = NaN; 255 256 else 257 if ddelim=='.', 258 t(t==ddelim)='.'; 259 end; 260 if FLAG_OCTAVE, %% Octave 261 [v,tmp2,c] = sscanf(char(t),'%f %s','C'); 262 elseif all(VER(1:2)=='3.') && any(VER(3)=='567'); %% FreeMat 3.5, 3.6, 3.7 263 [v,c,em] = sscanf(char(t),'%f %s'); 264 c = 1; 265 else %% Matlab 266 [v,c,em,ni] = sscanf(char(t),'%f %s'); 267 c = c * (ni>length(t)); 268 end; 269 if (c==1), 270 num(k1,k2) = v; 271 else 272 num(k1,k2) = NaN; 273 status(k1,k2) = -1; 274 end 275 end 276 end; 277end; 278end; 279 280