Mercurial > hg > octave-lyh
diff scripts/strings/strtrunc.m @ 13298:86d18a3cc911
strtrunc.m: Recode for 28X speedup for cellstr inputs
* strtrunc.m: Recode for 28X speedup for cellstr inputs
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 08 Oct 2011 12:37:34 -0700 |
parents | fd0a3ac60b0e |
children | ceda1714a4ad |
line wrap: on
line diff
--- a/scripts/strings/strtrunc.m +++ b/scripts/strings/strtrunc.m @@ -32,28 +32,29 @@ endif if (ischar (s)) - s_was_char = true; - s = {s}; - else - s_was_char = false; - endif - - if (iscellstr (s)) - for i = 1:(numel (s)) - s{i} = s{i}(:,1:(min (n, columns (s{i})))); - endfor + if (n < columns (s)) + s = s(:, 1:n); + endif + elseif (iscellstr (s)) + ## Convoluted approach converts cellstr to char matrix, trims the character + ## matrix using indexing, and then converts back to cellstr with mat2cell. + ## This approach is 28X faster than using cellfun and recursive call to strtrunc + idx = cellfun ("length", s) > n; + s(idx) = mat2cell (char (s(idx))(:, 1:n), ones (sum (idx), 1)); else error ("strtrunc: S must be a character string or a cell array of strings"); endif - if (s_was_char) - s = s{:}; - endif - endfunction -%!error <Invalid call to strtrunc> strtrunc (); -%!error <S must be a character string or a cell array of strings> strtrunc (1, 1) + %!assert (strtrunc("abcdefg", 4), "abcd"); %!assert (strtrunc("abcdefg", 10), "abcdefg"); +%!assert (strtrunc(char ("abcdef", "fedcba"), 3), ["abc"; "fed"]); %!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); + +%% Test input validation +%!error strtrunc () +%!error strtrunc ("abcd") +%!error strtrunc ("abcd", 4, 5) +%!error <S must be a character string or a cell array of strings> strtrunc (1, 1)