필터 지우기
필터 지우기

Calling class methods after implementing subsasgn

조회 수: 3 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by