Recursive data structure error in mex compiling of object-oriented program

2 views (last 30 days)
I am attempting to use Matlab Coder to build a mex function. The function itself is the front end for an object oriented program. I'm getting a compiling error and can reproduce it with a simple toy example.
The following example compiles and executes without errors. It consists of three files: the main function, an example parent class, and an example child class:
function str = main
M = ParentClass;
str = 'Done';
end
...
classdef ParentClass < handle
properties
Child;
end
methods
function self = ParentClass
self.Child = ChildClass;
end
end
end
...
classdef ChildClass < handle
properties
end
methods
function self = ChildClass
end
end
end
I'd like the child to keep a reference to its parent as a property, which is key for many design patterns. However, this causes a compiling error. I implement it by modifying the two classdefs as shown below. When the parent creates the child, the child stores the parent during the constructor.
classdef ParentClass < handle
properties
Child;
end
methods
function self = ParentClass
self.Child = ChildClass(self); % Line with the compiling error
end
end
end
...
classdef ChildClass < handle
properties
Parent
end
methods
function self = ChildClass(Parent)
self.Parent = Parent;
end
end
end
This produces the coder compiling error "This expression and its property 'Parent.Child' have the same class. Code generation does not support recursive data structures."
Is there a simple way around this language limitation? It works fine in the regular matlab environment, and this behavior is supported in other languages.
edit: Essentially, I'm looking for a way for the Child and Parent to have a reference to each other that is compatible with Matlab Coder.
Thanks in advance for the help
  1 Comment
Dominik Müller
Dominik Müller on 9 Nov 2020
Edited: Dominik Müller on 10 Nov 2020
Hi,
I know the question is already sth like 4 years old but is there an answer on this topic? I'm stuck to the same problem right now.

Sign in to comment.

Answers (1)

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 10 Nov 2020
Unfortunately MATLAB Coder does not support recursive data structures yet. It is documented in the below page :
We have made an internal request to support this in one of the future releases.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!