Matlab Coder App - undefined function when generating nested package

조회 수: 11 (최근 30일)
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.
D:\exampleproject\
LocalFunction.m
CallingScript.m
+foo\
NestedFunction.m
+bar\
DoubleNestedFunction.m
With the following contents: (don't mind the double nested one, even just having a package is enough for trouble)
% CallingScript.m
clear all
A = LocalFunction(5);
B = foo.NestedFunction(5);
% LocalFunction.m
function [outputArg1] = LocalFunction(inputArg1)
outputArg1 = inputArg1+1;
end
% +foo\NestedFunction.m
function [outputArg1] = NestedFunction(inputArg1)
outputArg1 = inputArg1*2;
end
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'.
Caused by:
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
Darshan Ramakant Bhat 2021년 9월 9일
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.
Workaround :
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);
end
Then you can use "foo_wrapper" as the entrypoint.
You can specify a namespace in the generated C++ code using below feature :
  댓글 수: 2
sacoene
sacoene 2021년 9월 9일
Thank you for confirming the limitation. Might I suggest adding this somewhere in the documentation and/or making the GUI (and even commandline codegen output) error more descriptive?
I'm aware of the linked page/method (e.g. here would be a perfect place to mention that package functions can't be used as entry-points) but like I mentioned, I was hoping for some more structure in my code, so I'm looking forward for that future release.
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021년 9월 9일
Thank you for the feedback, I will make a request to add this limitation in the document.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Generating Code에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by