comparison m-toolbox/classes/+utils/@prog/wrapstring.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function s = wrapstring(s, n)
2 % WRAPSTRING wraps a string to a cell array of strings with each cell less than n characters long.
3 %
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 %
6 % DESCRIPTION: WRAPSTRING wraps a string to a cell array of strings with each
7 % cell less than n characters long.
8 %
9 % CALL: s = wrapstring(s, n)
10 %
11 % INPUTS: s - String
12 % n - max length of each cell
13 %
14 % OUTPUTS: s - the wrapped cell string
15 %
16 % VERSION: $Id: wrapstring.m,v 1.1 2008/06/18 13:35:11 hewitson Exp $
17 %
18 % HISTORY: 05-02-2007 M Hewitson
19 % Creation
20 %
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22
23 % convert to a cell if necessary
24 if ischar(s)
25 s = [cellstr(s)];
26 end
27
28 % check it makes sense to proceed.
29 x = splitstring(s{end}, n);
30 if strcmp(char(x), char(s))
31 return
32 end
33
34 % now keep splitting until we are happy.
35 while length(s{end}) >= n
36 if length(s) > 1
37 s1 = s(1:end-1);
38 else
39 s1 = [];
40 end
41 s2 = splitstring(s{end},n);
42 if isempty(s2)
43 break
44 end
45 s = [s1 s2];
46 end
47
48 %--------------------------------------------------------------------------
49 % split string at first ' ' or ','
50 function s = splitstring(s,n)
51
52
53 % disp(sprintf('- splitting %s', s))
54 fi = 0;
55 idx = find(s==' ' | s == ',' | s == '(' | s == '=');
56 if max(idx) > n
57 for i=1:length(idx)
58 if idx(i) > n & fi==0
59 fi = i;
60 end
61 end
62 j = idx(fi);
63 s = [cellstr(strtrim(s(1:j))) cellstr(strtrim(s(j+1:end)))];
64 else
65 s = [];
66 return;
67 end