Mercurial > hg > octave-lyh
annotate scripts/strings/strjust.m @ 9207:25f50d2d76b3
improve TR updating strategy for fminunc and fsolve
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 17 May 2009 17:54:51 +0200 |
parents | 58604c45ca74 |
children | fb8834c12035 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle |
3789 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3789 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3789 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} strjust (@var{s}, ["left"|"right"|"center"]) | |
21 ## Shift the non-blank text of @var{s} to the left, right or center of | |
22 ## the string. If @var{s} is a string array, justify each string in the | |
23 ## array. Null characters are replaced by blanks. If no justification | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## is specified, then all rows are right-justified. For example: |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
25 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
26 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
27 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
28 ## strjust (["a"; "ab"; "abc"; "abcd"]) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
29 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
30 ## a |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
31 ## ab |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
32 ## abc |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
33 ## abcd |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
34 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
35 ## @end example |
3789 | 36 ## @end deftypefn |
37 | |
38 function x = strjust (x, just) | |
39 | |
40 if (nargin < 1 || nargin > 2) | |
6046 | 41 print_usage (); |
3789 | 42 endif |
43 | |
44 if (nargin == 1) | |
45 just = "right"; | |
46 endif | |
47 | |
48 just = tolower (just); | |
49 | |
5568 | 50 ## convert nulls to blanks |
51 idx = find (toascii (x) == 0); | |
52 if (! isempty (idx)) | |
53 x(idx) = " "; | |
54 endif | |
3789 | 55 |
5568 | 56 ## For all cases, left, right and center, the algorithm is the same. |
57 ## Find the number of blanks at the left/right end to determine the | |
58 ## shift, rotate the row index by using mod with that shift, then | |
59 ## translate the shifted row index into an array index. | |
60 [nr, nc] = size (x); | |
61 idx = (x' != " "); | |
62 if (strcmp (just, "right")) | |
63 [N, hi] = max (cumsum (idx)); | |
64 shift = hi; | |
65 elseif (strcmp (just, "left")) | |
66 [N, lo] = max (cumsum (flipud (idx))); | |
67 shift = (nc - lo); | |
68 else | |
69 [N, hi] = max (cumsum (idx)); | |
70 [N, lo] = max (cumsum (flipud (idx))); | |
71 shift = ceil (nc - (lo-hi)/2); | |
72 endif | |
73 idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc); | |
74 x = x (idx*nr + [1:nr]'*ones(1,nc)); | |
3789 | 75 |
76 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
77 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
78 %!error <Invalid call to strjust> strjust(); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
79 %!error <Invalid call to strjust> strjust(["a";"ab"], "center", 1); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
80 %!assert (strjust (["a"; "ab"; "abc"; "abcd"]), |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
81 %! [" a";" ab"; " abc"; "abcd"]); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
82 %!assert (strjust (["a"; "ab"; "abc"; "abcd"], "center"), |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
83 %! [" a "; " ab"; "abc "; "abcd"]); |