Daniel@0: function sT = som_table_modify(sT,action,arg1,arg2,arg3) Daniel@0: Daniel@0: %SOM_TABLE_MODIFY Modify table: add or remove columns or rows. Daniel@0: % Daniel@0: % sTable = som_table_modify(sTable,action,arg1,[arg2],[arg3]) Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % sTable (struct) table struct Daniel@0: % action (string) action id (see below). Daniel@0: % arg1 (varies) Depending on action, 1 to 3 arguments Daniel@0: % [arg2] (varies) are needed. See below. Daniel@0: % [arg3] (varies) Daniel@0: % Daniel@0: % sTable (struct) the modified table struct Daniel@0: % Daniel@0: % Actions and their arguments: Daniel@0: % 'addcol' Add one or several new columns. Daniel@0: % arg1 (cell array) new values Daniel@0: % (char) new values (a single column can be given) Daniel@0: % (matrix) new values Daniel@0: % arg2 (cell array) new headers Daniel@0: % arg3 (scalar) at which position the new columns Daniel@0: % should be inserted (at the end by default) Daniel@0: % 'addrow' Add one or several new rows. Daniel@0: % arg1 (cell array) new values Daniel@0: % (char) new values (a single row can be given) Daniel@0: % (matrix) new values Daniel@0: % arg2 (scalar) at which position the new rows Daniel@0: % should be inserted (at the end by default) Daniel@0: % 'removecol' Remove one or several columns. Daniel@0: % arg1 (vector) indeces of columns to be removed Daniel@0: % 'removerow' Remove one or several rows. Daniel@0: % arg1 (vector) indeces of rows to be removed Daniel@0: % Daniel@0: % See also SOM_TABLE_STRUCT, SOM_TABLE_PRINT. Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, January 4th, 2002 by Juha Vesanto Daniel@0: % Copyright (c) by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta juuso 040102 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: [nrT,ncT] = size(sT.values); Daniel@0: Daniel@0: switch action, Daniel@0: case 'addcol', Daniel@0: values = arg1; Daniel@0: if ischar(values), values = cellstr(values); end Daniel@0: if isnumeric(values), values = num2cell(values); end Daniel@0: spans = ones([size(values) 2]); Daniel@0: [nr,nc] = size(values); Daniel@0: if nargin<4, header = cell(1,nc); header(:) = {''}; else header = arg2; end Daniel@0: if ischar(header), header = cellstr(header); end Daniel@0: if nargin<5, where = ncT+1; else where = arg3; end Daniel@0: if nrT ~= nr, Daniel@0: error('Mismatch between sizes of given table and additional columns') Daniel@0: else Daniel@0: sT.headers = [sT.headers(:,1:where-1), header, sT.headers(:,where:end)]; Daniel@0: sT.values = [sT.values(:,1:where-1), values, sT.values(:,where:end)]; Daniel@0: sT.span = [sT.span(:,1:where-1,:), spans, sT.span(:,where:end,:)]; Daniel@0: end Daniel@0: case 'addrow', Daniel@0: values = arg1; Daniel@0: if ischar(values), values = cellstr(values); end Daniel@0: if isnumeric(values), values = num2cell(values); end Daniel@0: [nr,nc] = size(values); Daniel@0: spans = ones([size(values) 2]); Daniel@0: if nargin<4, where = nrT+1; else where = arg2; end Daniel@0: if ncT ~= nc, Daniel@0: error('Mismatch between sizes of given table and additional rows') Daniel@0: else Daniel@0: sT.values = [sT.values(1:where-1,:); values; sT.values(where:end,:)]; Daniel@0: sT.span = [sT.span(1:where-1,:,:); spans; sT.span(where:end,:,:)]; Daniel@0: end Daniel@0: case 'removecol', Daniel@0: where = setdiff(1:ncT,arg1); Daniel@0: sT.values = sT.values(:,where); Daniel@0: sT.headers = sT.headers(:,where); Daniel@0: sT.span = sT.span(:,where,:); Daniel@0: case 'removerow', Daniel@0: where = setdiff(1:nrT,arg1); Daniel@0: sT.values = sT.values(where,:); Daniel@0: sT.headers = sT.headers(where,:); Daniel@0: sT.span = sT.span(where,:,:); Daniel@0: end Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%