Skip to content

Commit

Permalink
Remove mp_table.copy(), add note about functional syntax for mp_table…
Browse files Browse the repository at this point in the history
… method calls.

And add tests for isempty() and istable() methods.
  • Loading branch information
rdzman committed Dec 20, 2023
1 parent 46a4721 commit d37bacb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
55 changes: 17 additions & 38 deletions lib/mp_table.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
% is compatible with MATLAB's built-in table class. Other features,
% such as table joining, etc., are not implemented.
%
% .. important::
%
% Since the dot syntax ``T.<var_name>`` is used to access table variables,
% you must use a functional syntax ``<method>(T,...)``, as opposed to
% the object-oriented ``T.<method>(...)``, to call mp_table methods.
%
% mp_table Methods:
% * mp_table - construct object
% * copy - create a duplicate of the object
% * istable - true for mp_table objects
% * size - dimensions of table
% * isempty - true if table has no columns or no rows
Expand Down Expand Up @@ -94,53 +99,24 @@
end
end

function new_obj = copy(obj)
% Create a duplicate of the object.
% ::
%
% new_T = T.copy();
new_obj = eval(class(obj)); %% create new object

%% make copies of properties
if have_feature('octave')
s1 = warning('query', 'Octave:classdef-to-struct');
warning('off', 'Octave:classdef-to-struct');
end
props = fieldnames(obj);
if have_feature('octave')
warning(s1.state, 'Octave:classdef-to-struct');
end
for k = 1:length(props)
new_obj.(props{k}) = obj.(props{k});
end

%% make copies of values
for k = 1:length(obj.Properties.VariableValues)
if isobject(obj.Properties.VariableValues{k}) && ...
ismethod(obj.Properties.VariableValues{k}, 'copy')
new_obj.Properties.VariableValues{k} = ...
copy(obj.Properties.VariableValues{k});
end
end
end

function TorF = istable(obj)
% Returns true.
% ::
%
% TorF = istable(T);
%
% Unfortunately, this is not really useful until Octave
% implements a built-in istable() that this can override.
%
% See also istable.
TorF = true;
end

function varargout = size(obj, dim)
% Returns dimensions of table.
% ::
%
% [m, n] = T.size();
% m = T.size(1);
% n = T.size(2);
% [m, n] = size(T);
% m = size(T, 1);
% n = size(T, 2);
varargout = cell(1, nargout);
w = length(obj.Properties.VariableValues);
if w
Expand All @@ -167,6 +143,9 @@

function TorF = isempty(obj)
% Returns ``true`` if the table has no columns or no rows.
% ::
%
% TorF = isempty(T)
TorF = prod(size(obj)) == 0;
end

Expand Down Expand Up @@ -642,7 +621,7 @@ function display(obj)
end
fprintf('\n');
end
end %% methods
end %% methods (public)

methods (Access=protected)
function [var_names, row_names, dim_names, args] = ...
Expand All @@ -667,5 +646,5 @@ function display(obj)
end
end
end
end %% methods
end %% methods (protected)
end %% classdef
10 changes: 9 additions & 1 deletion lib/t/t_mp_table.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
verbose = 1;
end

nt = 166;
nt = 169;
skip_tests_for_tablicious = 1;
table_classes = {@mp_table};
class_names = {'mp_table'};
Expand Down Expand Up @@ -86,6 +86,14 @@
t_is(size(T(4,6)), [1 1], 12, [t 'T(i,j) : size']);
% show_me(T(5,4));

t = sprintf('%s : istable()', cls);
t_ok(istable(T), t);

t = sprintf('%s : isempty() : ', cls);
t_ok(~isempty(T), [t 'false']);
T1 = table_class();
t_ok(isempty(T1), [t 'true']);

t = sprintf('%s : constructor - ind vars, w/names : ', cls);
if skip_oct_tab || skip_ml_tab
T = table_class(var1, var2, var3, var4, var5, var6, ...
Expand Down

0 comments on commit d37bacb

Please sign in to comment.