Home > utils > linewrap.m

linewrap

PURPOSE ^

LINEWRAP Separate a single string into multiple strings

SYNOPSIS ^

function c = linewrap(s, maxchars)

DESCRIPTION ^

LINEWRAP Separate a single string into multiple strings
   C = LINEWRAP(S, MAXCHARS) separates a single string into multiple
   strings by separating the input string, S, on word breaks.  S must be a
   single-row char array. MAXCHARS is a nonnegative integer scalar
   specifying the maximum length of the broken string.  C is a cell array
   of strings.

   C = LINEWRAP(S) is the same as C = LINEWRAP(S, 80).

   Note: Words longer than MAXCHARS are not broken into separate lines.
   This means that C may contain strings longer than MAXCHARS.

   This implementation was inspired a blog posting about a Java line
   wrapping function:
   http://joust.kano.net/weblog/archives/000060.html
   In particular, the regular expression used here is the one mentioned in
   Jeremy Stein's comment.

   Example
       s = 'Image courtesy of Joe and Frank Hardy, MIT, 1993.'
       c = linewrap(s, 40)

   See also TEXTWRAP.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function c = linewrap(s, maxchars)
0002 %LINEWRAP Separate a single string into multiple strings
0003 %   C = LINEWRAP(S, MAXCHARS) separates a single string into multiple
0004 %   strings by separating the input string, S, on word breaks.  S must be a
0005 %   single-row char array. MAXCHARS is a nonnegative integer scalar
0006 %   specifying the maximum length of the broken string.  C is a cell array
0007 %   of strings.
0008 %
0009 %   C = LINEWRAP(S) is the same as C = LINEWRAP(S, 80).
0010 %
0011 %   Note: Words longer than MAXCHARS are not broken into separate lines.
0012 %   This means that C may contain strings longer than MAXCHARS.
0013 %
0014 %   This implementation was inspired a blog posting about a Java line
0015 %   wrapping function:
0016 %   http://joust.kano.net/weblog/archives/000060.html
0017 %   In particular, the regular expression used here is the one mentioned in
0018 %   Jeremy Stein's comment.
0019 %
0020 %   Example
0021 %       s = 'Image courtesy of Joe and Frank Hardy, MIT, 1993.'
0022 %       c = linewrap(s, 40)
0023 %
0024 %   See also TEXTWRAP.
0025 
0026 % Steven L. Eddins
0027 % $Revision: 1.7 $  $Date: 2006/02/08 16:54:51 $
0028 
0029 error(nargchk(1, 2, nargin));
0030 
0031 bad_s = ~ischar(s) || (ndims(s) > 2) || (size(s, 1) ~= 1);
0032 if bad_s
0033    error('S must be a single-row char array.');
0034 end
0035 
0036 if nargin < 2
0037    % Default value for second input argument.
0038    maxchars = 80;
0039 end
0040 
0041 % Trim leading and trailing whitespace.
0042 s = strtrim(s);
0043 
0044 % Form the desired regular expression from maxchars.
0045 exp = sprintf('(\\S\\S{%d,}|.{1,%d})(?:\\s+|$)', maxchars, maxchars);
0046 
0047 % Interpretation of regular expression (for maxchars = 80):
0048 % '(\\S\\S{80,}|.{1,80})(?:\\s+|$)'
0049 %
0050 % Match either a non-whitespace character followed by 80 or more
0051 % non-whitespace characters, OR any sequence of between 1 and 80
0052 % characters; all followed by either one or more whitespace characters OR
0053 % end-of-line.
0054 
0055 tokens = regexp(s, exp, 'tokens').';
0056 
0057 % Each element if the cell array tokens is single-element cell array
0058 % containing a string.  Convert this to a cell array of strings.
0059 get_contents = @(f) f{1};
0060 c = cellfun(get_contents, tokens, 'UniformOutput', false);
0061 
0062 % Remove trailing whitespace characters from strings in c.  This can happen
0063 % if multiple whitespace characters separated the last word on a line from
0064 % the first word on the following line.
0065 c = deblank(c);
0066 
0067

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003