Calling class methods after implementing subsasgn

4 ビュー (過去 30 日間)
Erik Johannes Loo
Erik Johannes Loo 2022 年 5 月 23 日
回答済み: Erik Johannes Loo 2022 年 5 月 24 日
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 Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by