]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/asci.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / asci.m
1 ## Copyright (C) 2008, Thomas Treichl <thomas.treichl@gmx.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function} {[@var{string}] =} asci ([@var{columns}])
18 ## Print ASCI table.
19 ##
20 ## If this function is called without any input argument and without any output
21 ## argument then print a nice ASCI-table (excluding special characters with
22 ## hexcode 0x00 to 0x20) on screen with four columns per default. If this
23 ## function is called with one output argument then return an ASCI-table string
24 ## and don't print anything on screen. Finally, if this function is called with
25 ## one input argument of type scalar then either print (no output argument) or
26 ## return (one output argument) an ASCI-table with a number of columns given in
27 ## @var{columns}.
28 ##
29 ## For example,
30 ## @example
31 ## A = asci (3);
32 ## disp (A);
33 ## @end example
34 ## @end deftypefn
35
36 function [varargout] = asci (varargin)
37
38   %# Check number and types of input arguments
39   if (nargin == 0)
40     vcol = 4;
41   elseif (isnumeric (varargin{1}) && \
42           isequal (size (varargin{1}), [1, 1]))
43     vcol = floor (varargin{1});
44   else
45     print_usage ();
46   endif
47
48   %# First char is #32 (0x20) and last char is #128 (0x80)
49   vtab = "";
50   voff = floor ((128 - 32) / vcol);
51
52   %# Print a first row for the and underline that row
53   for vcnt = 1:vcol
54     vtab = sprintf ("%s Dec Hex Chr ", vtab);
55   endfor
56   vtab = sprintf ("%s\n", vtab);
57
58   for vcnt = 1:vcol
59     vtab = sprintf ("%s-------------", vtab);
60   endfor
61   vtab = sprintf ("%s\n", vtab);
62
63   %# Create the lines and columns of the asci table
64   for vpos = 32:(32+voff)
65     for vcnt = 1:vcol
66       vact = (vcnt-1)*voff+vpos;
67       vstr = {num2str(vact), dec2hex(vact), char(vact)};
68       for vctn = 1:length (vstr)
69         vtab = sprintf ("%s %3s", vtab, vstr{vctn});
70       endfor
71       vtab = sprintf ("%s ", vtab);
72     endfor
73     vtab = sprintf ("%s\n", vtab);
74   endfor
75   vtab = sprintf ("%s\n", vtab);
76
77   %# Print table to screen or return it to output argument
78   if (nargout == 0)
79     printf ("%s", vtab);
80   elseif (nargout == 1)
81     varargout{1} = vtab;
82   endif
83 endfunction
84
85 %!test
86 %!  A = asci ();
87 %!test
88 %!  A = asci (2);