-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
8fe7645
commit 81b2c42
Showing
9 changed files
with
368 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
function varargout = removevars(varargin) | ||
nargoutchk(0, 1); | ||
narginchk(1, 2); | ||
T = varargin{1}; | ||
mustBeA(T, 'table', 1); | ||
if (nargin < 2) | ||
vars = []; | ||
else | ||
vars = varargin{2}; | ||
end | ||
T(:, vars) = []; | ||
varargout{1} = T; | ||
end | ||
%============================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
function varargout = renamevars(varargin) | ||
narginchk(3, 3); % Check that there are exactly 3 input arguments | ||
nargoutchk(0, 1); % Check that there is at most 1 output argument | ||
|
||
% Unpack input arguments | ||
T = varargin{1}; | ||
oldNames = varargin{2}; | ||
newNames = varargin{3}; | ||
|
||
% Ensure the types are correct | ||
mustBeA(T, 'table', 1); % Check if T is a table | ||
mustBeText(oldNames, 2); % Check if oldNames are text | ||
mustBeText(newNames, 3); % Check if newNames are text | ||
|
||
% Convert oldNames and newNames to cell arrays of strings | ||
oldNames = cellstr(oldNames); | ||
newNames = cellstr(newNames); | ||
|
||
% Ensure oldNames and newNames are the same length | ||
if (length(oldNames) ~= length(newNames)) | ||
error(_('oldNames and newNames must have the same length.')); | ||
end | ||
|
||
% Check for duplicates in oldNames | ||
if (length(unique(oldNames)) ~= length(oldNames)) | ||
error('Duplicate names in oldNames are not allowed.'); | ||
end | ||
|
||
% Check for duplicates in newNames | ||
if (length(unique(newNames)) ~= length(newNames)) | ||
error('Duplicate names in newNames are not allowed.'); | ||
end | ||
|
||
% Convert table to struct | ||
st = struct(T); | ||
|
||
% Get current fieldnames from st.data | ||
currentFields = fieldnames(st.data); | ||
|
||
% Check if all oldNames exist in the current fieldnames | ||
for i = 1:length(oldNames) | ||
if ~any(strcmp(oldNames{i}, currentFields)) | ||
error(sprintf(_('Name "%s" not found in the table.'), oldNames{i})); | ||
end | ||
end | ||
|
||
% Create new struct for renamed fields | ||
newData = struct(); | ||
|
||
% Copy and rename fields | ||
for i = 1:length(currentFields) | ||
currentField = currentFields{i}; | ||
idx = find(strcmp(currentField, oldNames)); | ||
|
||
if ~isempty(idx) | ||
% If field should be renamed, use new name | ||
newData.(newNames{idx}) = st.data.(currentField); | ||
else | ||
% If field is not in oldNames, keep original name | ||
newData.(currentField) = st.data.(currentField); | ||
end | ||
end | ||
|
||
% Replace old data struct with new one | ||
st.data = newData; | ||
|
||
% Update VariableNames in Properties | ||
oldVarNames = st.Properties.VariableNames; | ||
newVarNames = oldVarNames; | ||
for i = 1:length(oldNames) | ||
idx = strcmp(oldNames{i}, oldVarNames); | ||
if any(idx) | ||
newVarNames{idx} = newNames{i}; | ||
end | ||
end | ||
st.Properties.VariableNames = newVarNames; | ||
|
||
varargout{1} = class(st, 'table'); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xmldoc> | ||
<copyright>SAME AS NELSON SOFTWARE</copyright> | ||
|
||
<language>en_US</language> | ||
<keyword>removevars</keyword> | ||
<short_description>Delete variables from table.</short_description> | ||
|
||
<syntax> | ||
<syntax_item>TB = removevars(TA, varsNames)</syntax_item> | ||
</syntax> | ||
|
||
<param_input> | ||
|
||
<param_input_item> | ||
<param_name>TA</param_name> | ||
<param_description>Input table.</param_description> | ||
</param_input_item> | ||
<param_input_item> | ||
<param_name>varsNames</param_name> | ||
<param_description | ||
>Variable names in input table to remove: character vector, string array or cell array of character vectors.</param_description> | ||
</param_input_item> | ||
|
||
</param_input> | ||
|
||
<param_output> | ||
<param_output_item> | ||
<param_name>TB</param_name> | ||
<param_description>Table object modified.</param_description> | ||
</param_output_item> | ||
</param_output> | ||
|
||
<description> | ||
<p><b>TB = removevars(TA, varsNames)</b> removes the variables specified by <b | ||
>varsNames</b> from the table <b | ||
>TA</b> and stores the remaining variables in <b>T2</b>.</p> | ||
<p | ||
>You can specify the variables by name, position, or using logical indices.</p> | ||
<p>You can also remove variables from a table using <b | ||
>T(:, varsNames) = []</b>.</p> | ||
</description> | ||
<used_function /> | ||
<bibliography /> | ||
|
||
<examples> | ||
|
||
<example_item> | ||
<example_item_type>nelson</example_item_type> | ||
<example_item_description /> | ||
<example_item_data | ||
><![CDATA[C = {'John', 28, true; 'Alice', 35, false; 'Bob', 42, true}; | ||
% Convert the cell array to a table | ||
T1 = cell2table(C) | ||
T2 = removevars(T1, 'C2') | ||
]]> | ||
</example_item_data> | ||
</example_item> | ||
|
||
</examples> | ||
|
||
<see_also> | ||
<see_also_item> | ||
<link linkend="${table}table">table</link> | ||
</see_also_item> | ||
<see_also_item> | ||
<link linkend="${table}renamevars">renamevars</link> | ||
</see_also_item> | ||
|
||
</see_also> | ||
|
||
<history> | ||
<history_item> | ||
<history_version>1.9.0</history_version> | ||
<history_description>initial version</history_description> | ||
</history_item> | ||
</history> | ||
|
||
<authors> | ||
<author_item>Allan CORNET</author_item> | ||
</authors> | ||
</xmldoc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xmldoc> | ||
<copyright>SAME AS NELSON SOFTWARE</copyright> | ||
|
||
<language>en_US</language> | ||
<keyword>renamevars</keyword> | ||
<short_description>Rename variables in table.</short_description> | ||
|
||
<syntax> | ||
<syntax_item>TB = renamevars(TA, varsNames, newNames)</syntax_item> | ||
</syntax> | ||
|
||
<param_input> | ||
|
||
<param_input_item> | ||
<param_name>TA</param_name> | ||
<param_description>Input table.</param_description> | ||
</param_input_item> | ||
<param_input_item> | ||
<param_name>varsNames</param_name> | ||
<param_description | ||
>Variable names in input table: character vector, string array or cell array of character vectors.</param_description> | ||
</param_input_item> | ||
<param_input_item> | ||
<param_name>newNames</param_name> | ||
<param_description | ||
>New names for variables: character vector, string array or cell array of character vectors.</param_description> | ||
</param_input_item> | ||
|
||
</param_input> | ||
|
||
<param_output> | ||
<param_output_item> | ||
<param_name>TB</param_name> | ||
<param_description | ||
>Table object with variable names modified.</param_description> | ||
</param_output_item> | ||
</param_output> | ||
|
||
<description> | ||
<p><b | ||
>TB = renamevars(TA, varsNames, newNames)</b> renames the variables in the table <b | ||
>TA</b> as specified by <b | ||
>varsNames</b> and assigns them the new names provided in <b | ||
>newNames</b>.</p> | ||
<p | ||
>You can also rename all the variables in a table by assigning new names to its <b | ||
>VariableNames</b> property using <b | ||
>T.Properties.VariableNames = newNames</b>.</p> | ||
<p>In this case, <b | ||
>newNames</b> must be a string array or a cell array of character vectors.</p> | ||
</description> | ||
<used_function /> | ||
<bibliography /> | ||
|
||
<examples> | ||
|
||
<example_item> | ||
<example_item_type>nelson</example_item_type> | ||
<example_item_description /> | ||
<example_item_data | ||
><![CDATA[C = {'John', 28, true; 'Alice', 35, false; 'Bob', 42, true}; | ||
% Convert the cell array to a table | ||
T1 = cell2table(C); | ||
T2 = renamevars(T1, {'C1', 'C2'}, {'Name', 'Age'}) | ||
T3 = cell2table(C); | ||
T3.Properties.VariableNames = {'Name', 'Age', 'Married'}; | ||
T3]]> | ||
</example_item_data> | ||
</example_item> | ||
|
||
</examples> | ||
|
||
<see_also> | ||
<see_also_item> | ||
<link linkend="${table}table">table</link> | ||
</see_also_item> | ||
<see_also_item> | ||
<link linkend="${table}removevars">removevars</link> | ||
</see_also_item> | ||
|
||
</see_also> | ||
|
||
<history> | ||
<history_item> | ||
<history_version>1.9.0</history_version> | ||
<history_description>initial version</history_description> | ||
</history_item> | ||
</history> | ||
|
||
<authors> | ||
<author_item>Allan CORNET</author_item> | ||
</authors> | ||
</xmldoc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'}; | ||
Age = [38;43;38;40;49]; | ||
Smoker = logical([1;0;1;0;1]); | ||
Height = [71;69;64;67;64]; | ||
Weight = [176;163;131;133;119]; | ||
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80]; | ||
T = table(LastName, Age, Smoker, Height, Weight, BloodPressure); | ||
R = removevars(T, 'Age'); | ||
REF = table(LastName, Smoker, Height, Weight, BloodPressure); | ||
assert_isequal(R, REF); | ||
R2 = removevars(R, 1); | ||
REF = table(Smoker, Height, Weight, BloodPressure); | ||
assert_isequal(R2, REF); | ||
%============================================================================= | ||
R = removevars(T,{'Smoker','Weight'}); | ||
REF = table(LastName, Age, Height, BloodPressure); | ||
assert_isequal(R, REF); | ||
%============================================================================= |
Oops, something went wrong.