Skip to content

Commit d0525b4

Browse files
committed
write: enhance management of Tables without row names
1 parent acc843b commit d0525b4

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

matStats/@Table/createRowNames.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
function createRowNames(obj, varargin)
1+
function rowNames = createRowNames(obj, varargin)
22
% Create default row names for table.
33
%
4-
% createRowNames(TAB)
4+
% NAMES = createRowNames(TAB)
5+
% Creates an array of unique name for each row, as a NROWS-by-1 cell
6+
% array. Default is to create a cell array of char representing index of
7+
% each row: NAMES = {'1', '2', '3', ...}'.
8+
%
9+
% NAMES = createRowNames(TAB, PATTERN)
10+
% Uses the specified pattern for creating row names. Default pattern is
11+
% '%d'.
512
%
613
% Example
714
% data = reshape(1:12, [3 4])';
815
% tab = Table(data, {'C1', 'C2', 'C3'});
9-
% createRowNames(tab, 'row%02d');
16+
% tab.RowNames = createRowNames(tab, 'row%02d');
1017
% tab
1118
% tab =
1219
% C1 C2 C3
@@ -16,13 +23,13 @@ function createRowNames(obj, varargin)
1623
% row04 10 11 12
1724
%
1825
% See also
19-
% create, parseFactorFromRowNames
26+
% create, write, parseFactorFromRowNames
2027

2128
% ------
2229
% Author: David Legland
23-
% e-mail: david.legland@inra.fr
30+
% e-mail: david.legland@inrae.fr
2431
% Created: 2019-12-12, using Matlab 9.7.0.1247435 (R2019b) Update 2
25-
% Copyright 2019 INRA - Cepia Software Platform.
32+
% Copyright 2019 INRAE - Cepia Software Platform.
2633

2734
nr = size(obj.Data, 1);
2835

@@ -31,4 +38,4 @@ function createRowNames(obj, varargin)
3138
format = varargin{1};
3239
end
3340

34-
obj.RowNames = strtrim(cellstr(num2str((1:nr)', format)));
41+
rowNames = strtrim(cellstr(num2str((1:nr)', format)));

matStats/@Table/write.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function write(obj, fileName, varargin)
6666
% default values of parameters
6767
format = [];
6868
writeHeader = ~isempty(obj.ColNames);
69+
rowNames = obj.RowNames;
6970
writeRowNames = ~isempty(obj.RowNames);
7071
writeLevels = hasFactors(obj) ;
7172
sep = ' ';
@@ -106,6 +107,11 @@ function write(obj, fileName, varargin)
106107
nRows = size(obj.Data, 1);
107108
nCols = size(obj.Data, 2);
108109

110+
% if need to write row names without valid row names, ensure valid ones.
111+
if writeRowNames && isempty(rowNames)
112+
rowNames = createRowNames(obj);
113+
end
114+
109115
% compute default format string for writing data, if not given as argument
110116
if isempty(format)
111117
format = ['%g' repmat(' %g', 1, nCols-1) '\n'];
@@ -176,12 +182,8 @@ function write(obj, fileName, varargin)
176182
end
177183

178184
% add '%s ' in the beginning if missing
179-
if nTokens ~= nCols + 1 && ~isempty(obj.RowNames)
180-
% len = -1;
181-
% for i = 1:nRows
182-
% len = max(len, length(obj.rowNames{i}));
183-
% end
184-
len = max(cellfun(@length, obj.RowNames));
185+
if nTokens ~= nCols + 1 && writeRowNames
186+
len = max(cellfun(@length, rowNames));
185187
format = ['%-' int2str(len) 's ' format];
186188
end
187189

@@ -201,15 +203,16 @@ function write(obj, fileName, varargin)
201203

202204
% write the header line
203205
if writeHeader
204-
% initialize first row with default tag
205-
str = 'name';
206-
207206
% write the names of the columns, separated by spaces
208-
for i = 1:nCols
209-
str = [str headerSep obj.ColNames{i}]; %#ok<AGROW>
210-
end
207+
pattern = ['%s' repmat([headerSep '%s'], 1, nCols-1) '\\n'];
208+
str = sprintf(pattern, obj.ColNames{:});
211209

212-
str = [str '\n'];
210+
% optionnally adds column name for row names
211+
if writeRowNames
212+
str = ['name' headerSep str];
213+
end
214+
215+
% print header to file
213216
fprintf(f, str);
214217
end
215218

@@ -218,7 +221,7 @@ function write(obj, fileName, varargin)
218221
% write data as numeric
219222
if writeRowNames
220223
for i = 1:nRows
221-
fprintf(f, format, obj.RowNames{i}, obj.Data(i, :));
224+
fprintf(f, format, rowNames{i}, obj.Data(i, :));
222225
end
223226
else
224227
for i = 1:nRows
@@ -241,7 +244,7 @@ function write(obj, fileName, varargin)
241244

242245
% write current row
243246
if writeRowNames
244-
fprintf(f, format, obj.RowNames{i}, data{:});
247+
fprintf(f, format, rowNames{i}, data{:});
245248
else
246249
fprintf(f, format, data{:});
247250
end

0 commit comments

Comments
 (0)