Resetting a memoized function

25 views (last 30 days)
Matt J
Matt J about 16 hours ago
Edited: Matt J about 14 hours ago
I know that clearCache will clear the cache of a MemoizedFunction, but how would you reset its other properties to their defaults? I would have thought that rebuilding it with memoize() would do so, but the test below shows that that does not work.
mf=memoize(@localFcn) %default property values
mf =
MemoizedFunction with properties: Function: @localFcn Enabled: 1 CacheSize: 10
mf.CacheSize=3; mf.Enabled=false %make some non-default property settings
mf =
MemoizedFunction with properties: Function: @localFcn Enabled: 0 CacheSize: 3
clear mf
mf=memoize(@localFcn) %clear and rebuild -- but property values do not reset!!!!
mf =
MemoizedFunction with properties: Function: @localFcn Enabled: 0 CacheSize: 3
function y=localFcn(x)
y=x.^2;
end

Answers (1)

Walter Roberson
Walter Roberson 11 minutes ago
According to +matlab/+lang/MemoizedFunction.m
% 2. MemoizedFunction objects are persistent to a session of MATLAB.
% For Example:
% f = memoize(@plus);
% f.Enabled = 0; % By default, Enabled == true
% clear f; % Only clears the object f.
% h = memoize(@plus);
% isequal( h.Enabled, false ); % State that was set by 'f'
So this is by design.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!