Code genaration - function reuse with different variables size

2 ビュー (過去 30 日間)
Y
Y 2014 年 12 月 21 日
コメント済み: Ryan Livingston 2014 年 12 月 21 日
Hi,
I have 2 matlab coder projects and I wish to use the same function in both of them (the exact one, not a duplicate).
Let's say, for example, that my function looks like that:
function a = Foo
a = randn(SIZE_OF_VAR);
where 'SIZE_OF_VAR' is some constant.
In Project A, 'Foo' is one of many entry point functions. In Project B, 'Foo' is only called from other function and is not required to be an entry point function.
My problem is that 'SIZE_OF_VAR' must be different in Project A and Project B. I know that in Project B I can pass it as an input argument. But in Project A it should be hard coded, as far as I know (variable sizing and dynamic memory allocation are not an option).
Is there anyway that it can be achieved?

回答 (2 件)

Stephen23
Stephen23 2014 年 12 月 21 日
It seems that you are looking for a way to set a function's default values:
  1 件のコメント
Y
Y 2014 年 12 月 21 日
No, actually not. I am looking for a way to set constant values in a function that would be different according to some code generation configuration or something, but independent of the inputs.

サインインしてコメントする。


Ryan Livingston
Ryan Livingston 2014 年 12 月 21 日
編集済み: Ryan Livingston 2014 年 12 月 21 日
You could declare your function:
function a = Foo(N)
a = randn(N);
In project B just call the function how you want. In project A, where you want the size to be hard-coded you could generate code like:
codegen Foo -args {coder.Constant(100)} -config:lib -report
In the Project GUI you can click "Define constant" to do the same thing:
  2 件のコメント
Y
Y 2014 年 12 月 21 日
Ok, thanks. And if Foo accepts more than one input and I do not wish to declare all inputs using -args? Any chance of using -args only for some of the inputs?
Ryan Livingston
Ryan Livingston 2014 年 12 月 21 日
All of the arguments need to be defined so that Coder knows how to produce C code for them. They don't all need to be constants. So if you have a function:
function y = foo(N,x)
y = randn(N);
y = y + x;
you could do:
codegen Foo -args {coder.Constant(100), 1} -report
That would treat N as a hard-coded constant 100 and allow x to be an input variable that is a real, 1-by-1 double.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenerating Code についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by