Mercurial > hg > octave-nkf
annotate scripts/strings/strtrunc.m @ 12451:c6c3fdbfede2
untabify configure.ac
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 15 Feb 2011 12:57:03 -0500 |
parents | fd0a3ac60b0e |
children | 86d18a3cc911 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2006-2011 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}) | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
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 | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
9036
diff
changeset
|
46 error ("strtrunc: S must be a character string or a cell array of strings"); |
5674 | 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 (); |
11473
44032aac5223
Correct failing error() tests due to change in capitalization of previous changeset.
Rik <octave@nomad.inbox5.com>
parents:
11472
diff
changeset
|
56 %!error <S must be a character string or a cell array of strings> strtrunc (1, 1) |
8442
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"}); |