Skip to content

Commit

Permalink
write: enhance management of Tables without row names
Browse files Browse the repository at this point in the history
  • Loading branch information
dlegland committed Mar 31, 2020
1 parent acc843b commit d0525b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
21 changes: 14 additions & 7 deletions matStats/@Table/createRowNames.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
function createRowNames(obj, varargin)
function rowNames = createRowNames(obj, varargin)
% Create default row names for table.
%
% createRowNames(TAB)
% NAMES = createRowNames(TAB)
% Creates an array of unique name for each row, as a NROWS-by-1 cell
% array. Default is to create a cell array of char representing index of
% each row: NAMES = {'1', '2', '3', ...}'.
%
% NAMES = createRowNames(TAB, PATTERN)
% Uses the specified pattern for creating row names. Default pattern is
% '%d'.
%
% Example
% data = reshape(1:12, [3 4])';
% tab = Table(data, {'C1', 'C2', 'C3'});
% createRowNames(tab, 'row%02d');
% tab.RowNames = createRowNames(tab, 'row%02d');
% tab
% tab =
% C1 C2 C3
Expand All @@ -16,13 +23,13 @@ function createRowNames(obj, varargin)
% row04 10 11 12
%
% See also
% create, parseFactorFromRowNames
% create, write, parseFactorFromRowNames

% ------
% Author: David Legland
% e-mail: david.legland@inra.fr
% e-mail: david.legland@inrae.fr
% Created: 2019-12-12, using Matlab 9.7.0.1247435 (R2019b) Update 2
% Copyright 2019 INRA - Cepia Software Platform.
% Copyright 2019 INRAE - Cepia Software Platform.

nr = size(obj.Data, 1);

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

obj.RowNames = strtrim(cellstr(num2str((1:nr)', format)));
rowNames = strtrim(cellstr(num2str((1:nr)', format)));
33 changes: 18 additions & 15 deletions matStats/@Table/write.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function write(obj, fileName, varargin)
% default values of parameters
format = [];
writeHeader = ~isempty(obj.ColNames);
rowNames = obj.RowNames;
writeRowNames = ~isempty(obj.RowNames);
writeLevels = hasFactors(obj) ;
sep = ' ';
Expand Down Expand Up @@ -106,6 +107,11 @@ function write(obj, fileName, varargin)
nRows = size(obj.Data, 1);
nCols = size(obj.Data, 2);

% if need to write row names without valid row names, ensure valid ones.
if writeRowNames && isempty(rowNames)
rowNames = createRowNames(obj);
end

% compute default format string for writing data, if not given as argument
if isempty(format)
format = ['%g' repmat(' %g', 1, nCols-1) '\n'];
Expand Down Expand Up @@ -176,12 +182,8 @@ function write(obj, fileName, varargin)
end

% add '%s ' in the beginning if missing
if nTokens ~= nCols + 1 && ~isempty(obj.RowNames)
% len = -1;
% for i = 1:nRows
% len = max(len, length(obj.rowNames{i}));
% end
len = max(cellfun(@length, obj.RowNames));
if nTokens ~= nCols + 1 && writeRowNames
len = max(cellfun(@length, rowNames));
format = ['%-' int2str(len) 's ' format];
end

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

% write the header line
if writeHeader
% initialize first row with default tag
str = 'name';

% write the names of the columns, separated by spaces
for i = 1:nCols
str = [str headerSep obj.ColNames{i}]; %#ok<AGROW>
end
pattern = ['%s' repmat([headerSep '%s'], 1, nCols-1) '\\n'];
str = sprintf(pattern, obj.ColNames{:});

str = [str '\n'];
% optionnally adds column name for row names
if writeRowNames
str = ['name' headerSep str];
end

% print header to file
fprintf(f, str);
end

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

% write current row
if writeRowNames
fprintf(f, format, obj.RowNames{i}, data{:});
fprintf(f, format, rowNames{i}, data{:});
else
fprintf(f, format, data{:});
end
Expand Down

0 comments on commit d0525b4

Please sign in to comment.