Resetting a memoized function

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

回答(2 个)

Walter Roberson
Walter Roberson 2026-2-25,1:25
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.

1 个评论

The behavior is clearly by design, but surely there are cases where you would want to restart the memoization process from scratch, without quitting Matlab. It seems the only way to do that is to manually set the properties every time you want to do so.

请先登录,再进行评论。

Catalytic
Catalytic 2026-2-25,20:00
编辑:Catalytic about 24 hours 前
One way to have the behavior you're talking about is to wrap the function in an anonymous function --
mf=memoize(@(x)localFcn(x)) %default property values
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 1 CacheSize: 10
mf.CacheSize=3; mf.Enabled=false %make some non-default property settings
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 0 CacheSize: 3
Now, when you rebuild the original anonymous function dies so the memoization starts fresh --
clear mf
mf=memoize(@(x)localFcn(x)) %clear and rebuild
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 1 CacheSize: 10
function y=localFcn(x)
y=x.^2;
end

1 个评论

But you can't do it this way because a reference fh to the function handle lingers in the workspace throughout --
fh=@(x)localFcn(x);
mf=memoize(fh) %default property values
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 1 CacheSize: 10
mf.CacheSize=3; mf.Enabled=false %make some non-default property settings
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 0 CacheSize: 3
clear mf
mf=memoize(fh)
mf =
MemoizedFunction with properties: Function: @(x)localFcn(x) Enabled: 0 CacheSize: 3
function y=localFcn(x)
y=x.^2;
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Performance and Memory 的更多信息

产品

版本

R2024b

标签

提问:

2026-2-24,23:43

编辑:

2026-2-25,20:04

Community Treasure Hunt

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

Start Hunting!

Translated by