Processing a batch of data every T seconds in Simulink

2 views (last 30 days)
I have set up an enviroment for the simulation of the attitude determination of a satellite in simulink.
The enviroment is split, for now, in two chunks, the first one deals with the creations of the measurements, the second one deals with the attitude determination algorithm.
The situation is as follows: every Ts seconds, a sequence of N measurements is obtained, where N is the number of sensors. These measurements are stored in a matrix like form using the "Buffer" block. The output matrix is a MxN matrix, where M = T/Ts and T is the averaging window length.
What I want to do is: every T seconds I want to process the whole batch of data in a subsytem and obtain an estimate for the attitude. The estimate is recursive, meaning that whenever a new batch of data is obtained it shall be added to the previous one to better refine the estimate.
My issues are: The buffer initial state cannot be set to "NaN", thus I always have an initial value entering in the subsystem at t=0 that I don't want. I wander what is the best solution to achieve recursive data batch estimation every T seconds in simulink.

Accepted Answer

Adarsh
Adarsh on 6 May 2025
I understand that you are trying to run an attitude determination algorithm for a satellite with the data collected from numerous sensors after every batch of ‘M’ samples collected.
You can achieve this task by utilizing, “MATLAB Function” and “Triggered Subsystem” block.
Firstly, you can implement the attitude determination algorithm which you want to perform after every batch in the “Triggered Subsystem” block and you can write a function inside a “MATLAB Function” block keeping the “data” variable in which all the data will be stored as “persistent” as it has to maintain its state over various function calls and updating it after each sample time.
You can use a Boolean variable “trigger” which is true only when you get a batch of ‘M’ samples.
Now you can connect the inputs from sensors to “MATLAB Function” block’s input ports and connect the “trigger” signal to the trigger port of the “Triggered Subsystem” and connect the “data” output from the “MATLAB Function” block to “Triggered Subsystem”.
Now as you know the size of the “data” signal will vary as time progresses, hence you must right click on the block, select explore and from model explorer click on data variable and check the “Variable size” box and mention the maximum size of the signal in the “size” field.
Here is a sample model for the same workflow where I am taking average of samples from a sine wave (Here the precision improves as the number of samples increases and average moves close to 0):
Code for the “MATLAB Function” block:
function [data,trigger,countval] = recursive_estimator(new_batch)
persistent all_data
persistent initialized
persistent batch_size
persistent count
if isempty(all_data) % Initially all_data is empty so when first sample arrives we add it to all_data and reduce
% batch count from 100 to 99 as I am taking 100 as batch size
all_data = new_batch';
batch_size = 99;
count = 1;
end
trigger = false;
all_data = [all_data;new_batch'];
batch_size = batch_size-1;
data = all_data;
count = count+1;
countval = count;
if batch_size==0 % Here batch size means remaining elements needed to complete the batch so here we have to reset
batch_size = 100;
trigger = true;
end
Here is the model explorer for the “MATLAB Function” block:
After this setup, you can run the simulation to get the required results.
For more information, you can refer to the following documentation links:
  1. https://www.mathworks.com/help/releases/R2024b/simulink/slref/matlabfunction.html
  2. https://www.mathworks.com/help/releases/R2024b/simulink/ug/triggered-subsystems.html
I hope it helps!

More Answers (1)

Niccolò
Niccolò on 13 Jun 2025
Hi @Adarsh,
sorry for the delay in my reply. Your solution is incredible! It works really well and it's really well explained. Thanks for the clarifications, I've learnt a lot from this!

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!