Effacer les filtres
Effacer les filtres

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

5 vues (au cours des 30 derniers jours)
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 commentaire
Dyuman Joshi
Dyuman Joshi le 7 Nov 2023
Modifié(e) : Dyuman Joshi le 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.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 27 Avr 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.

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by