Declare C Array in C Function dependent on constant

48 views (last 30 days)
Dear Matlab-Team,
I would like to include some custom C code into my Simulink model using the C Caller / C function block. As I need some intermediate variables, I would like to declare a C array dependent on the size of the input "LEN", which is constant.
float data[LEN];
As C only allows direct values or macros, I cannot use the constant in the ports and parameters section. I would like to avoid to pre allocated temporarily needed arrays in Simulink and then feed as reference to my function. Also I need to avoid dynamic memory allocation like malloc.
Is there any option to declare a macro for example that can be then used in the C function?
Thanks for your support!

Accepted Answer

TED MOSBY
TED MOSBY on 21 Nov 2024 at 10:20
You must load ‘LEN’ in the base workspace first, otherwise it will throw error. You can do this by simply writing LEN=5 in the MATLAB command window.
You can then follow the steps as shown below. I used an example code. You can define a macro in the ‘defines’ section as shown below:
Below is the example model I used to check if this was working :
Don’t forget to add the ports and parameters in the block parameters section like this:
Let me know if this solves your query or not!
  1 Comment
Simon Hafner
Simon Hafner on 23 Nov 2024 at 10:53
Thanks! Even though this not exactly what I planned. But it's a viable work around. Thanks!

Sign in to comment.

More Answers (1)

aditi bagora
aditi bagora on 20 Nov 2024 at 11:33
Hi Simon,
I understand that you want to define a macro to specify the array length for use in the 'C Function' block. Simulink offers a convenient way to achieve this through the 'Defines' section in the configuration panel. Please refer to the steps below to implement this approach:
  1. Navigate to Model Configuration Parameters and select Simulation Target.
  2. In the Custom Code section, find the Defines option.
  3. Define your macro here, for instance: LEN=100.
  4. Once you have applied these configuration settings, you can easily use the macro within your 'C Function' block.
Please refer to the following MathWorks documentation for more details on defining macros : https://www.mathworks.com/help/simulink/gui/defines.html
Hope this helps!
  2 Comments
Simon Hafner
Simon Hafner on 20 Nov 2024 at 16:02
Hi,
Thanks for your answer! Is it possible to make the LEN = 100 dependent on a constant, which can be changed programmatically?
aditi bagora
aditi bagora on 21 Nov 2024 at 5:16
Hi Simon,
I see that you want the LEN to change programmatically, macros are usually used to define constants. If you want the value to change programmatically then maybe you should use a variable but that cannot be used in your case as you eventually need it for array declaration in C.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!