Home > m > helper > wrapstring.m

wrapstring

PURPOSE ^

WRAPSTRING wraps a string to a cell array of strings with each cell less

SYNOPSIS ^

function s = wrapstring(s, n)

DESCRIPTION ^

 WRAPSTRING wraps a string to a cell array of strings with each cell less
 than n characters long.
 
 Usage: s = wrapstring(s, n)
 
 M Hewitson 05-02-07
 
 $Id: wrapstring.html,v 1.1 2007/06/08 14:15:10 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function s = wrapstring(s, n)
0002 % WRAPSTRING wraps a string to a cell array of strings with each cell less
0003 % than n characters long.
0004 %
0005 % Usage: s = wrapstring(s, n)
0006 %
0007 % M Hewitson 05-02-07
0008 %
0009 % $Id: wrapstring.html,v 1.1 2007/06/08 14:15:10 hewitson Exp $
0010 %
0011 
0012 % convert to a cell if necessary
0013 if ischar(s)
0014   s = [cellstr(s)];
0015 end
0016 
0017 % check it makes sense to proceed.
0018 x = splitstring(s{end}, n);
0019 if strcmp(char(x), char(s))
0020   return
0021 end
0022 
0023 % now keep splitting until we are happy.
0024 while length(s{end}) >= n
0025   if length(s) > 1
0026     s1 = s(1:end-1);
0027   else
0028     s1 = [];
0029   end
0030   s2 = splitstring(s{end},n);
0031   if isempty(s2)
0032     break
0033   end
0034   s = [s1 s2];
0035 end
0036 
0037 %--------------------------------------------------------------------------
0038 % split string at first ' ' or ','
0039 function s = splitstring(s,n)
0040 
0041 
0042 % disp(sprintf('- splitting %s', s))
0043 fi = 0;
0044 idx = find(s==' ' | s == ',' | s == '(' | s == '=');
0045 if max(idx) > n
0046   for i=1:length(idx)
0047     if idx(i) > n & fi==0
0048       fi = i;
0049     end
0050   end
0051   j = idx(fi);
0052   s = [cellstr(strtrim(s(1:j))) cellstr(strtrim(s(j+1:end)))];
0053 else
0054   s = [];
0055   return;
0056 end

Generated on Fri 08-Jun-2007 16:09:11 by m2html © 2003