Clear Filters
Clear Filters

Call C code function from Stateflow with an array as an input

54 views (last 30 days)
I have a C-code function that takes in an array and a size value. I am trying to call the function in Stateflow and I am not having any success no matter what I try. I did add the appropriate includes to the C-code in the model config under simulation target/custom code. The only way I can get Stateflow to update/compile is when I change the C-code to not take in an array, but just a single element.
This is the function call I am trying to use:
The function ElbowFindFxn() works as a Simulink custom code block, if finds and uses the external C-code just fine.
The variable vecV is defined as a 100 element float array (max size, use less than that in practice). I've tried passing in the array in multiple different ways:
&vecV[0]
(float*)vecV
vecV[] - this gave a syntax error right away, but was worth a shot
and a few others I don't remember.
I even tried putting the function into a Stateflow graphical function.
For the most part I don't get just a single error, but the compiler seems to blow up and spill its guts.
I know it's not the C-code, because that code has worked just fine in other applications, and in Simulink custom code block.

Answers (1)

Rajanya
Rajanya on 20 Sep 2024 at 7:43
I understand that you are trying to call a C function which takes an array as an argument, from a Stateflow chart.
Custom code functions with non-scalar or pointer arguments are not directly supported for function call in MATLAB based blocks. Stateflow suggests us to use ‘coder.ceval’ to call such functions instead.
To demonstrate the working, I have created a sample C function ‘myExternalFunction’ which takes in an array and returns the second element in the array. The array is sent as input to a Stateflow state, and it is called from that state to display the output. The function and the Stateflow chart are shown below.
‘coder.rref’ passes the input array as a reference. If the function takes in other arguments, they are added in the ‘coder.ceval’ call with comma separations after the reference is passed.
After the proper configurations are added in Simulation target/Custom code and the model is updated, the simulation runs properly. For demonstration purposes, the array is passed as input from a constant block. The array passed is [1 2 3 4].
For more information on ‘coder.ceval’, refer - https://www.mathworks.com/help/simulink/slref/coder.ceval.html
Since the documentation for MATLAB R2018b is not available online, you can use the command to view the same by running it in command window.
web(fullfile(docroot, 'simulink/slref/coder.ceval.html'))
Hope this helps!
  1 Comment
Ken Seiders
Ken Seiders on 20 Sep 2024 at 17:36
This does not seem to work for me, perhaps because for my model the Stateflow action language is C.
I tried it in a .m file and that seemed to accept it.
I get the error:
Unresolved function 'coder.ceval'

Sign in to comment.

Categories

Find more on Complex Logic in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!