Use variable size data with Matlab function block in Simulink
6 views (last 30 days)
I have a function that uses a structure data as input and saves the data after modifying it (the size of the structure elements may change). I implemented the function in a "Interpreted Matlab function" in Simulink and everything works perfect. for example:
I used the same method for a couple of blocks and the data is basically shared between them. the block loads the data from workspace at each sampling time and by using assignin at the end of the function it saves the data to workspace again.
Simulink "Interpreted Matlab function" is not supported by Matlab coder so instead I have to use "Matlab function" block to do the same thing. (because I want to use Simulink Real time)
There are two solutions that I found but none of the works.
- using data store memory. the problem is I have to specify the struct as a bus and it has to be fixed size. (variable size bus is not supported)
- setting a parameter in the scope (ports and data manager of the function block) but the parameter is loaded from workspace at each sampling time and can not be saved (assignin or save is not supported)
please let me know if anyone has any idea
Denis Gurchenkov on 22 Feb 2018
One possible solution is to "emulate" variable-sized data using a fixed-size buffer and a separate field for size. That is, create a Data Store Memory block that contains a structure (let's call it "data") with two fields, a_data and a_size. Make a_data to be of some large size (say, 1024 elements), and initialize a_size with 0.
Then, inside the MATLAB function block (presuming that the variable "data" is the global that is mapped to that Data Store Memory) do
data.a_data[data.a_size] = 1; data.a_size = data.a_size+1;
And in another block, to read the last element
x = data.a_data[data.a_size];
Note that you would need to handle cases where a_size is 0 (no data have been added) and a_size == numel(a_data) (there is no more room in the a_data array).