1 function resu = display(df)
3 %# function resu = display(df)
4 %# Tries to produce a nicely formatted output of a dataframe.
6 %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
8 %% This file is part of Octave.
10 %% Octave is free software; you can redistribute it and/or
11 %% modify it under the terms of the GNU General Public
12 %% License as published by the Free Software Foundation;
13 %% either version 2, or (at your option) any later version.
15 %% Octave is distributed in the hope that it will be useful,
16 %% but WITHOUT ANY WARRANTY; without even the implied
17 %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 %% PURPOSE. See the GNU General Public License for more
21 %% You should have received a copy of the GNU General Public
22 %% License along with Octave; see the file COPYING. If not,
23 %% write to the Free Software Foundation, 51 Franklin Street -
24 %% Fifth Floor, Boston, MA 02110-1301, USA.
27 %# $Id: display.m 9585 2012-02-05 15:32:46Z cdemills $
30 %# generate header name
31 dummy = inputname (1);
36 if (2 == length (df._cnt))
37 head = sprintf ("%s = dataframe with %d rows and %d columns", \
40 head = sprintf ("%s = dataframe with %d rows and %d columns on %d pages", \
44 if (!isempty (df._src))
45 for indi = (1:size (df._src, 1))
47 (head, [repmat("Src: ", size (df._src{indi, 1}, 1), 1)\
52 if (!isempty (df._cmt))
53 for indi = (1:size(df._cmt, 1))
55 (head, [repmat("Comment: ", size (df._cmt{indi, 1}, 1), 1)\
60 if (all (df._cnt > 0)) %# stop for empty df
61 dummy=[]; vspace = repmat (' ', df._cnt(1), 1);
62 indi = 1; %# the real, unfolded index
63 %# loop over columns where the corresponding _data really exists
64 for indc = (1:min (df._cnt(2), size (df._data, 2)))
65 %# emit column names and type
66 if (1 == length (df._rep{indc}))
67 dummy{1, 2+indi} = deblank (disp (df._name{2}{indc}));
68 dummy{2, 2+indi} = deblank (df._type{indc});
70 %# append a dot and the third-dimension index to column name
71 tmp_str = [deblank(disp (df._name{2}{indc})) "."];
72 tmp_str = arrayfun (@(x) horzcat (tmp_str, num2str(x)), ...
73 (1:length (df._rep{indc})), 'UniformOutput', false);
74 dummy{1, 2+indi} = tmp_str{1};
75 dummy{2, 2+indi} = deblank (df._type{indc});
76 for indk = (2:length (tmp_str))
77 dummy{1, 1+indi+indk} = tmp_str{indk};
78 dummy{2, 1+indi+indk} = dummy{2, 2+indi};
81 %# "print" each column
84 indk = 1; while (indk <= size (df._data{indc}, 2))
85 tmp_str = df._data{indc}(:, indk); %#get the whole column
86 indj = cellfun ('isprint', tmp_str, 'UniformOutput', false);
87 indj = ~cellfun ('all', indj);
88 for indr = (1:length(indj))
90 if (isna (tmp_str{indr})),
93 if (~ischar (tmp_str{indr}))
94 tmp_str{indr} = char (tmp_str{indr});
96 tmp_str{indr} = undo_string_escapes (tmp_str{indr});
100 %# keep the whole thing, and add a vertical space
101 dummy{3, 2+indi} = disp (char (tmp_str));
102 dummy{3, 2+indi} = horzcat...
103 (vspace, char (regexp (dummy{3, 2+indi}, '.*', ...
104 'match', 'dotexceptnewline')));
105 indi = indi + 1; indk = indk + 1;
108 %# keep only one horizontal space per line
109 unfolded = df._data{indc}(:, df._rep{indc});
110 indk = 1; while (indk <= size (unfolded, 2))
111 dummy{3, 2+indi} = disp (unfolded(:, indk));
112 tmp_str = char (regexp (dummy{3, 2+indi}, \
113 '[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(\s??[-+]\s??[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?i)?', \
114 'match', 'dotexceptnewline'));
116 (vspace, char (regexp (dummy{3, 2+indi}, '\S.*', ...
117 'match', 'dotexceptnewline')));
118 dummy{3, 2+indi} = tmp_str;
119 indi = indi + 1; indk = indk + 1;
124 %# put everything together
125 vspace = [' '; ' '; vspace];
126 %# second line content
128 if (!isempty (df._ridx))
129 for (ind1 = 1:size (df._ridx, 2))
130 if ((1 == size(df._ridx, 3)) && \
131 (any (!isna (df._ridx(1:df._cnt(1), ind1)))))
132 dummy{2, 1} = [sprintf("_%d", ind1) ; "Nr"];
133 dummy{3, 1} = disp (df._ridx(1:df._cnt(1), ind1));
134 indi = regexp (dummy{3, 1}, '\b.*\b', 'match', 'dotexceptnewline');
136 resu = strjust (char (dummy{2, 1}, indi), 'right');
138 resu = horzcat(resu, vspace, strjust (char (dummy{2, 1}, indi), \
142 for ind2 = (1:size (df._ridx, 3))
143 if (any (!isna (df._ridx(1:df._cnt(1), ind1, ind2)))),
144 dummy{2, 1} = [sprintf("_%d.%d", ind1, ind2) ; "Nr"];
145 dummy{3, 1} = disp (df._ridx(1:df._cnt(1), ind1, ind2));
146 indi = regexp (dummy{3, 1}, '\b.*\b', 'match', 'dotexceptnewline');
148 resu = strjust (char (dummy{2, 1}, indi), 'right');
150 resu = horzcat (resu, vspace, strjust (char(dummy{2, 1}, indi), \
160 if (isempty (df._name{1})),
161 dummy{2, 2} = []; dummy{3, 2} = [];
163 dummy{2, 2} = [" ";" "];
164 dummy{3, 2} = df._name{1};
167 %# insert a vertical space
168 if (!isempty(dummy{3, 2}))
169 indi = ~cellfun ('isempty', dummy{3, 2});
172 resu = horzcat (resu, vspace, strjust (char(dummy{2, 2}, dummy{3, 2}),\
175 disp ('line 172 '); keyboard
181 for indi = (1:size (dummy, 2) - 2)
182 %# was max(df._cnt(2:end)),
184 %# avoid this column touching the previous one
185 if (any (cellfun ('size', dummy(1:2, 2+indi), 2) >= \
186 size (dummy{3, 2+indi}, 2)))
187 resu = horzcat (resu, vspace);
189 resu = horzcat (resu, strjust (char (dummy{:, 2+indi}), 'right'));
191 tmp_str = sprintf ("Emitting %d lines, expecting %d", ...
192 size (dummy{3, 2+indi}, 1), df._cnt(1));
201 resu = char (head, resu); disp (resu)