Resetting a memoized function
Ältere Kommentare anzeigen
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.CacheSize=3; mf.Enabled=false %make some non-default property settings
clear mf
mf=memoize(@localFcn) %clear and rebuild -- but property values do not reset!!!!
function y=localFcn(x)
y=x.^2;
end
Antworten (2)
Walter Roberson
am 25 Feb. 2026 um 1:25
0 Stimmen
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 Kommentar
Matt J
am 25 Feb. 2026 um 17:44
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.CacheSize=3; mf.Enabled=false %make some non-default property settings
Now, when you rebuild the original anonymous function dies so the memoization starts fresh --
clear mf
mf=memoize(@(x)localFcn(x)) %clear and rebuild
function y=localFcn(x)
y=x.^2;
end
1 Kommentar
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.CacheSize=3; mf.Enabled=false %make some non-default property settings
clear mf
mf=memoize(fh)
function y=localFcn(x)
y=x.^2;
end
Kategorien
Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!