Clear Filters
Clear Filters

How to create variable length buffer in Simulink for holding the value with unknown dimensions ?

5 views (last 30 days)
i want to store a value of one signal in Simulink until the my trigger becomes high. Every time when trigger becomes high, the number of values of signal between two triggers are different.
i tried to use the following approach in MATLAB function block but it didn't worked
function = func(trigger , SignalA)
persistent store;
if isempty(store)
store=[];
end
if(trigger ==0)
store= [store SignalA];
end
end
it gives me following error
Assigning empty arrays to persistent variables is not supported.
Any other method to do this thing?
  1 Comment
Dyuman Joshi
Dyuman Joshi on 7 Nov 2023
Edited: Dyuman Joshi on 7 Nov 2023
The variable store is empty at the moment you are checking for the same.
Why reassign it?
It's redundant. Remove the if condition block.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Apr 2024
You are going to have trouble with that approach unless you use dynamic memory allocation.
Better is to set a maximum length between triggers, and use something more like
function buffer = func(trigger, SignalA )
Buffer_Length = 20480;
persistent store counter
if isempty(store)
counter = 0;
store = zeros(1, Buffer_Length);
end
if trigger == 0
if counter <= Buffer_Length
counter = counter + 1;
store(1, counter) = SignalA;
else
%do nothing, drop the sample
end
buffer = [];
else
buffer = store(1:counter);
counter = 0;
store = zeros(1, Buffer_Length);
end
end
As long as trigger is 0, this records the signal; if the recording would be too long then it drops the samples.
As soon as the trigger becomes non-zero, it releases all of the stored samples in one go, and resets the buffer.

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!