Home > m > helper > wrapstring.m

wrapstring

PURPOSE ^

WRAPSTRING wraps a string to a cell array of strings with each cell less than n characters long.

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.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: WRAPSTRING wraps a string to a cell array of strings with each
              cell less than n characters long.

 CALL:       s = wrapstring(s, n)

 INPUTS:     s  - String
             n  - max length of each cell

 OUTPUTS:    s  - the wrapped cell string

 VERSION: $Id: wrapstring.m,v 1.4 2007/07/13 12:17:39 ingo Exp $

 HISTORY: 05-02-2007 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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 than n characters long.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: WRAPSTRING wraps a string to a cell array of strings with each
0007 %              cell less than n characters long.
0008 %
0009 % CALL:       s = wrapstring(s, n)
0010 %
0011 % INPUTS:     s  - String
0012 %             n  - max length of each cell
0013 %
0014 % OUTPUTS:    s  - the wrapped cell string
0015 %
0016 % VERSION: $Id: wrapstring.m,v 1.4 2007/07/13 12:17:39 ingo Exp $
0017 %
0018 % HISTORY: 05-02-2007 M Hewitson
0019 %             Creation
0020 %
0021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0022 
0023 % convert to a cell if necessary
0024 if ischar(s)
0025   s = [cellstr(s)];
0026 end
0027 
0028 % check it makes sense to proceed.
0029 x = splitstring(s{end}, n);
0030 if strcmp(char(x), char(s))
0031   return
0032 end
0033 
0034 % now keep splitting until we are happy.
0035 while length(s{end}) >= n
0036   if length(s) > 1
0037     s1 = s(1:end-1);
0038   else
0039     s1 = [];
0040   end
0041   s2 = splitstring(s{end},n);
0042   if isempty(s2)
0043     break
0044   end
0045   s = [s1 s2];
0046 end
0047 
0048 %--------------------------------------------------------------------------
0049 % split string at first ' ' or ','
0050 function s = splitstring(s,n)
0051 
0052 
0053 % disp(sprintf('- splitting %s', s))
0054 fi = 0;
0055 idx = find(s==' ' | s == ',' | s == '(' | s == '=');
0056 if max(idx) > n
0057   for i=1:length(idx)
0058     if idx(i) > n & fi==0
0059       fi = i;
0060     end
0061   end
0062   j = idx(fi);
0063   s = [cellstr(strtrim(s(1:j))) cellstr(strtrim(s(j+1:end)))];
0064 else
0065   s = [];
0066   return;
0067 end

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003