I'm using coder to generate C source code. Of course many matlab functions are not available for coder. But I still want to use these functions when running the code in matlab.
Currently I have to comment out all these sections that are not coder-friendly when doing C source code generation. Is there something I can wrap them in to make the coder ignore them?

댓글 수: 1

Sean de Wolski
Sean de Wolski 2013년 11월 4일
Alex, please contact technical support and let them know what functions you would like code generation support for.

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

 채택된 답변

Mike Hosea
Mike Hosea 2013년 10월 28일

6 개 추천

You can wrap them with
if coder.target('MATLAB')
...
end
Or if that gives an error,
if isempty(coder.target)
...
end
You can also declare unsupported functions as "extrinsic" and use them in generated mex files. This practice has some pitfalls, and where unsupported data structures are involved, it can rather involved and advanced. However, in the simpler cases it is very simple. If foobar() is MATLAB function that you are passing, say, an m-by-n matrix to, and foobar returns, say, a 1-by-n vector (for example, this is what SUM would do with an m-by-n matrix where neither m nor n is 1), you would write:
coder.extrinsic('foobar');
y = zeros(1,size(x,2));
y = foobar(x);
Or, if you don't want to mess with coder.extrinsic:
y = zeros(1,size(x,2));
y = feval('foobar',x);
The y = zeros(...) line looks like wasted effort, but what it really does is tell the compiler what to expect by the next line, so it can copy the data from the MATLAB return into local storage. Basically, if you know what a function will return given the type of the input, then you just initialize the output variable to the appropriate type and then call the function.

댓글 수: 7

Alex
Alex 2013년 11월 4일
Great info Mike, thanks. Works like a charm, and got me to understand this coder a bit more.
This is a great feature but somehow it does not work.
I am using the HDL Coder and escape an Inputparser with
if coder.target('MATLAB')
p = inputParser;
%...Inputparser Stuff
addOptional(p, 'variable', 'default', @(x)ischar(x));
%...Inputparser Stuff
else
%if nargin = ...
end
However the coder still complains about the anonymus function ischar() in that input parser section.
'@(x)ischar(x)' is an anonymous function. Anonymous functions are not supported for fixed-point conversion.
It seems, that the coder gets through the first stages fine, but complains later on. I want the coder to ignore
all of the code in coder.target('MATLAB').
Jan Siegmund
Jan Siegmund 2020년 3월 10일
편집: Jan Siegmund 2020년 3월 10일
You don't need an anonymous function to do this:
@(x)ischar(x)
Just specify a regular old function handle to the ischar function.
@ischar
Jan Siegmund
Jan Siegmund 2020년 3월 10일
Thanks @Steven,
I also had the fixed-point designer struggle with a "load(...)" function which was clearly escaped by "coder.target('MATLAB')". Using "coder.load(...)" will help here I guess?
Converting to a function handle is possible, but how about combined logic, like
@(x) ~isempty(x) && ~isvector(x)
?
Update: and even function handles are not supported:
'@ischar' function handles are not supported for fixed-point conversion.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Input Specification에 대해 자세히 알아보기

질문:

2013년 10월 25일

댓글:

2020년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by