I'm trying to convert a recursive function in Matlab to C/C++ code using Matlab's Coder app. In general, Coder gives an error saying that recursive functions are not allowed. However, the Matlab help pages say that you can generate code from recursive functions if you follow the below guidelines:
"When you use recursion in MATLAB code that is intended for code generation, follow these restrictions:"
- The top-level function in a MATLAB Function block cannot be a recursive function, but it can call a recursive function.
- Assign all outputs of a run-time recursive function before the first recursive call in the function.
- Assign all elements of cell array outputs of a run-time recursive function.
- Inputs and outputs of run-time recursive functions cannot be classes.
- The Maximum stack size parameter is ignored for run-time recursion.
I basically understand the conditions but I don't know how to get Matlab to accept the recursive code. I used one of the examples from Matlab:
function y = call_recursive()
n =35;
x = 10;
y = myrecursive(x,n);
end
function y = myrecursive(x,n)
coder.inline('never')
if x > 1
y = n + myrecursive(x-1,n-1);
else
y = n;
end
end
but I still get the "recursive functions not allowed" error. A stripped version of my own code is:
%------------------------
function recursion_test()
recursive_fn();
end %recursive caller
%------------------------
%------------------------
function recursive_fn()
persistent mycount;
if isempty(mycount) %initialize the persistent variable
mycount = 0;
end
counter = 0;
while(counter<300)
mycount = mycount + 1;
if mycount<100 %some generic test condition
recursive_fn();
end
if mycount<200 %another generic test condition
recursive_fn();
end %if
counter = counter+1;
end
disp(counter);
end %recursive function
%------------------------
My code gives the same error as Matlab's example. My specific application should never go into recursion more than 300 times...and usually is around 50-90 times. Note that the above code doesn't accurately keep track of the recursion but I do have tracking in the actual code. I could limit recursion to 300 times and force the compiler to make 300 copies of the recursion base code (I don't know how to do that...but that's a different problem for now). First, I would like to be able to generate C/C++ code with the Coder app using the above functions.
So, can anyone explain how to generate C/C++ code for the recursive functions above. Minimally, I would like call time recursion but ideally, I would like to know runtime recursion as well. Note I have gone through Matlab's code generation and recursion help pages and have searched for a solution using my favorite search engine however I haven't been able to find a clear, step-by-step procedure for it. Your help would be greatly appreciated.
std::stack<T>
whereT
is an item to hold all necessary input and output parameters. – πάντα ῥεῖcodegen recursive_test
on that code in MATLAB R2020a. What MATLAB release are you using? Codegen support for recursion was added in R2016b – Ryan Livingston