# HG changeset patch # User Rik # Date 1318124922 25200 # Node ID 29a4d3b4211ebc7735c3956c19c95c9e73f6a7ba # Parent ceda1714a4ad1189708626aef14a82a05ce8fc3d strtrunc.m: Add support and %!test for char arrays as cellstr elements * strtrunc.m: Add support and test for char arrays as cellstr elements diff -r ceda1714a4ad -r 29a4d3b4211e scripts/strings/strtrunc.m --- a/scripts/strings/strtrunc.m Sat Oct 08 20:33:50 2011 -0400 +++ b/scripts/strings/strtrunc.m Sat Oct 08 18:48:42 2011 -0700 @@ -19,10 +19,9 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} strtrunc (@var{s}, @var{n}) ## Truncate the character string @var{s} to length @var{n}. If @var{s} -## is a char matrix, then the number of columns is adjusted. -## +## is a character matrix, then the number of columns is adjusted. ## If @var{s} is a cell array of strings, then the operation is performed -## on its members and the new cell array is returned. +## on each cell element and the new cell array is returned. ## @end deftypefn function s = strtrunc (s, n) @@ -38,9 +37,10 @@ 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)); + ## This approach is 24X faster than using cellfun with call to strtrunc + idx = cellfun ("size", s, 2) > n; + rows = cellfun ("size", s(idx), 1); + s(idx) = mat2cell (char (s(idx))(:, 1:n), rows); else error ("strtrunc: S must be a character string or a cell array of strings"); endif @@ -52,10 +52,16 @@ %!assert (strtrunc("abcdefg", 10), "abcdefg"); %!assert (strtrunc(char ("abcdef", "fedcba"), 3), ["abc"; "fed"]); %!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); -%!assert (strtrunc({"1", "21", "321"}, 1), {"1", "2", "3"}) +%!test +%! cstr = {"line1"; ["line2"; "line3"]; "line4"}; +%! y = strtrunc (cstr, 4); +%! assert (size (y), [3, 1]); +%! assert (size (y{2}), [2, 4]); +%! assert (y{2}, repmat ("line", 2, 1)); %% Test input validation %!error strtrunc () %!error strtrunc ("abcd") %!error strtrunc ("abcd", 4, 5) %!error strtrunc (1, 1) +