Matlab Coder App - undefined function when generating nested package
6 views (last 30 days)
sacoene on 8 Sep 2021
I've been trying to generate C++ for a while now using the Matlab Coder App (GUI), but keep bumping into problems.
My final try was to convert the class-based structure to 'simple' functions, but keep a nested package hierarchy for organisational reasons.
Additionally, I created a Matlab script that is able to run the code without any issues.
Below is a 'minimal working example' that illustrates the problem.
With the following contents: (don't mind the double nested one, even just having a package is enough for trouble)
A = LocalFunction(5);
B = foo.NestedFunction(5);
function [outputArg1] = LocalFunction(inputArg1)
outputArg1 = inputArg1+1;
function [outputArg1] = NestedFunction(inputArg1)
outputArg1 = inputArg1*2;
The problem starts almost immediately, when trying to define the Entry-Point Functions:
Yes its shadowed, but that's the whole point... I've tried this after a fresh reboot of matlab as well, so it's not a problem with local workspace shadowing of the indicated function.
So instead I use the '...' to select the function file. This correctly adds the function(s) to the list.
However, in the next step, trying to run the script for auto-defining the variable names, the function names are not found:
Note that I have tried a lot of different things. Creating a new function handle with the name (thus removing the namespace), using import foo.*, but nothing works to get the script running.
So then I manually define the input types (is okay now, but a lot of work for the actual project).
Then the next problem is the MEX generation (or actually the execution):
Error preparing the test expression 'CallingScript'.
Compiled function 'D:\exampleproject\NestedFunction_mex.mexw64' is not in the same folder as the MATLAB function 'D:\exampleproject\+foo\NestedFunction.m'.
Not surprising since the output of that step results in the following folder tree:
Ignoring the error and continuing does yield C++ code, but there the package namespace is removed. Plus I'm not able to verify the right execution with the MEX function.
The only way I've seen to retain the namespace is by calling the nested function from the local one, and not defining the nested function as entry-point function... but that is not the way I want to go, since I'm specifically interested in library generation, with the (double) nested functions as entry points.
So should I conclude that directly generation namespaces from package functions (as entry-point functions) is impossible? Or is there something I'm missing?
In case it is not possible, I would like to put this in as feature request: generating a C++ library with namespace support by providing a package, rather than individual functions...
Darshan Ramakant Bhat on 9 Sep 2021
This is a limitation from MATLAB Coder. Entrypoint function should not be residing inside a package. We already have internal request to remove this limitiation in one of the future release.
Create a wrapper function which calls the package function internally like below :
function [y1,y2] = foo_wrapper(a,b)
y1 = foo.NestedFunction(a);
y2 = foo.bar.DoubleNestedFunction(b);
Then you can use "foo_wrapper" as the entrypoint.
You can specify a namespace in the generated C++ code using below feature :