Hi Rintu,
It requires a bit of a workaround because Simulink generally expects consistent sample times within a given subsystem. A combination of blocks that allow you to store data at one rate and then read it at a faster rate when needed can be used. You can follow the below MATLAB Function Block Implementation to achieve so:
- Initialization: Initialize a global variable or persistent variable within the MATLAB Function block to act as your data buffer.
- Writing Data (2 ms rate): On each execution at the slower rate, append the incoming data to your buffer.
- Reading Data (0.2 ms rate): Since you're reading faster than writing, manage the buffer size and handle cases where the buffer might be empty.
- Synchronize and Manage the Sampling Rate: Use control logic in Simulink that signals the MATLAB Function block when to read or write based on the simulation time. Ensure the part of your model that reads data operates at the faster rate.
Here's an example MATLAB Code to implement this:
function out = fifoBuffer(in, control, timestep)
persistent buffer bufferIndex;
buffer(bufferIndex) = in;
bufferIndex = bufferIndex + 1;
elseif control == 2 && bufferIndex > 1
buffer(1:bufferIndex-2) = buffer(2:bufferIndex-1);
bufferIndex = bufferIndex - 1;
In the above code, "control" is a flag that determines whether the block should store data ("control" == 1) or output data in FIFO mode ("control" == 2) and "timestep" could be used to adjust the operation based on the simulation time.
You may refer to the following documentation links to have a better understanding on sampling time:
- https://www.mathworks.com/help/simulink/ug/how-to-specify-the-sample-time.html
- https://www.mathworks.com/help/simulink/sample-time.html