Mercurial > hg > octave-lyh
annotate scripts/strings/strtrunc.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 502e58a0d44f |
children | 58604c45ca74 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2006, 2007, 2009 William Poetra Yoga Hadisoeseno |
5674 | 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. | |
5674 | 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/>. | |
5674 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} strtrunc (@var{s}, @var{n}) | |
21 ## Truncate the character string @var{s} to length @var{n}. If @var{s} | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
22 ## is a char matrix, then the number of columns is adjusted. |
5674 | 23 ## |
24 ## If @var{s} is a cell array of strings, then the operation is performed | |
25 ## on its members and the new cell array is returned. | |
26 ## @end deftypefn | |
27 | |
28 function s = strtrunc (s, n) | |
29 | |
30 if (nargin != 2) | |
6046 | 31 print_usage (); |
5674 | 32 endif |
33 | |
34 if (ischar (s)) | |
35 s_was_char = true; | |
36 s = {s}; | |
37 else | |
38 s_was_char = false; | |
39 endif | |
40 | |
41 if (iscellstr (s)) | |
42 for i = 1:(numel (s)) | |
43 s{i} = s{i}(:,1:(min (n, columns (s{i})))); | |
44 endfor | |
45 else | |
46 error ("strtrunc: s must be a character string or a cell array of strings"); | |
47 endif | |
48 | |
49 if (s_was_char) | |
50 s = s{:}; | |
51 endif | |
52 | |
53 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
54 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
55 %!error <Invalid call to strtrunc> strtrunc (); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
56 %!error <s must be a character string or a cell array of strings> strtrunc (1, 1) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
57 %!assert (strtrunc("abcdefg", 4), "abcd"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
58 %!assert (strtrunc("abcdefg", 10), "abcdefg"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
59 %!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); |