- Try obtaining a continuous relation between the output and simulation time. The “Clock” block provides the simulation time which may serve as input to the “C Caller”.
- If the output data is present as an array, then you may utilize array indexing within the C function. The “Stair Generator” block can be used to generate the array index at each time step, which serves as input to the “C Caller”. Here is the documentation for the “Stair Generator” block.
Problem returning arrays from a C Caller in Simulink
8 views (last 30 days)
Show older comments
I need to return some arrays inside a time based simulation from a C Code to Matlab.
I tried from a mex function that work and C Caller blocks that partially work, let me explain :
When calling the C function that do the simulation by itself (calling it once, without the timestep of simulink linked), it works. Now, I would like instead of one C call where all the time based simulation is done inside C, to dynamically call the C function for every time step of simulink (manually put inside config parameters). That means deleting a for loop that corresponds to the timestep in the C function and make the code behaves as each call of the simulation function return one pointer of the array instead of the entire array.
When comparing the non-linked timestep with the linked timestep model, when seeing the arrays in the workspace environment with to workspace blocks, instead of having for a 1D array, each data in one row, I have a diagonal matrix, where the datas are saved in diagonal, colums and rows now corresponding to time. It gives memory issues and given the size of my simulation, it can't be processed for the entire simulation. Also it seems to increase my windows temp files for every simulation, not really deleting it even though I use clearvars at the initFcn in the callbacks tab of Simulink.
Is this because of how C Caller work ? Am I missing some comprehension on how arrays are saved and returned ? Are there other options, like S functions ?
To put it more simply, if I want some 1D array, like [1,2,3] in C to be returned as each data corresponds to one timestep in Simulink, how can I do it while avoiding a diagonal matrix returned instead ?
0 Comments
Accepted Answer
Ashok
on 30 Sep 2024
Hi Julien,
I comprehend that you are trying to obtain a time-varying output from a “C Caller” block. This can be achieved in a couple of ways, which are as follows:
For instance, the attached “C_caller_example” ZIP file contains two C functions “functionOfTime” and “timeIdxMapped”. Both the functions produce the output, , where t is the simulation time. The “functionOfTime” function captures the relation through a continuous function whereas the “timeIdxMapped” function contains the output signal as a 1-D array.
You may refer the attached Simulink and C files for the implementations. Alternatively, you could use the “1-D Lookup Table” block in Simulink. Using this block, the output at a specific time can be obtained by interpolating a table of input-output data.
To learn more about "1-D Lookup Table", please refer to the following documentation:
I believe this will assist you!
More Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!