Why am I unable to use a constructor which returns an array of objects using MATLAB 7.6 (R2008a)?

3 views (last 30 days)
I am using constructor in a class which returns an array of objects. I receive the following error when I create an object of this class:
??? 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 ==> ArrayTest>ArrayTest.ArrayTest
I am following the syntax available at the following link:
<http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brd4btr.html>

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The constructor calls itself recursively with no arguments when the second row of objects is created.
To work around the issue, see the following example:
classdef ArrayTest < handle
properties
Quantity
end
methods
function t = ArrayTest(m,n)
if nargin == 0
return
end
for i = 1:m
for j = 1:n
t(i,j).Quantity = rand(10);
end
end
end
end
end
There is no workaround to create a no argument constructor which returns an array of objects.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!