Calling class methods after implementing subsasgn

1 view (last 30 days)
Hi,
I had this idea to write a minimal 'multi-dimensional' hash table.
Basically I was thinking of a way to classify days based on various parameters - but as I write this out - I realise I should probably just use a regular MATLAB table instead.
However, I will ask my question regardless if only to satisfy my curiosity and possibly help others who may have the same question.
How can I call a class' public method after implementing subsasgn/subsref?
It seems trying to call nDimHashTableInstance.size() gets captured by subsasgn. Should I modify subsasgn to redirect the call to the right function? That seems like a clunky solution.
Thanks
classdef nDimHashTable
properties (Access = private)
c = {}
end
methods (Access = public)
function this = nDimHashTable()
this.c = {};
end
function this = clear(this)
this.c = {};
end
function out = size(this)
out = sum(~isempty(this.c), 'all');
end
function this = subsasgn(this, key, value)
if key.type ~= "()"
error('Unrecognized property: %s', key.subs)
end
indices = nDimHashTable.hash(key.subs);
this.c{indices{:}} = value;
end
function out = subsref(this, key)
if key.type ~= "()"
error('Unrecognized property: %s', key.subs)
end
indices = nDimHashTable.hash(key.subs);
out = [this.c{indices{:}}];
end
end
methods (Static)
function out = hash(in)
out = {};
for key = string(in)
if key == ":"
out{end+1} = ':';
else
out{end+1} = mod(hex2dec(rptgen.hash(key)), 1979);
end
end
end
end
end

Accepted Answer

Erik Johannes Loo
Erik Johannes Loo on 24 May 2022

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by