Calling class methods after implementing subsasgn

3 次查看(过去 30 天)
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

采纳的回答

Erik Johannes Loo
Erik Johannes Loo 2022-5-24

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by