Use variable size data with Matlab function block in Simulink

조회 수: 2 (최근 30일)
milad karimshoushtari
milad karimshoushtari 2017년 12월 5일
답변: Denis Gurchenkov 2018년 2월 22일
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:
func(data)
data.a=[data.a 1];
assignin('base','data', data);
end
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.
The problem:
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.
  1. 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)
  2. 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

답변 (1개)

Denis Gurchenkov
Denis Gurchenkov 2018년 2월 22일
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).

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by