Why do I receive a "Maximum recursion limit of 500 reached" error when I try to SET or GET a dynamic property using an anonymous function in MATLAB 7.6 (R2008a)?

1 view (last 30 days)
I have a class defined as follows:
classdef testClassDynProps<dynamicprops
methods
function obj=testClassDynProps
metaProp = obj.addprop('testProp');
metaProp.SetMethod = @(obj,val) (testClassDynPropsSetMet(obj,val));
metaProp.GetMethod = @(obj) (testClassDynPropsGetMet(obj));
end % constructor
end
end
I invoke this as follows:
t=testClassDynProps;
t.testProp = 3; % this will result in infinite recursion
a = t.testProp; % this will result in infinite recursion
I receive the following error message:
??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.
Error in ==> testClassDynProps>@(obj,val)(testClassDynPropsSetMet(obj,val))
The Set and Get methods of a dynamic property created with addprops in a subclass of dynamicprops create infinite recursions, because the Get/Set functions call themselves.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Jul 2009
This change has been incorporated into the documentation in MATLAB 7.8 (R2009a).
For previous releases, read below for any additional information:
This is expected behavior based on how SET/GET functions and anonymous functions work. Only the SET/GET functions can SET/GET the actual property values. An anonymous function that calls another function to do the actual work cannot SET/GET the actual property value just as an ordinary SET/GET function cannot call another function to SET/GET the actual property.
To work around this issue, invoke the function as follows:
metaProp.SetMethod = @testClassDynPropsSetMet;
metaProp.GetMethod = @testClassDynPropsGetMet;

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!