You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
FIFO push all values as vector
6 views (last 30 days)
Show older comments
Hi, I want to pop the fifo every step time (1 sec) and push all the values as vector every 5 steps (5 sec). I didn’t find way to push more then one value every step time
Accepted Answer
Jon
on 11 Jul 2023
I assume you are using the Queue block in the DSP System Toolbox in Simulink.
If so you should supply the In port with a 5 element vector input, then supply the Push port with the output of Pulse Generator with 5 second period, and the Pop port with the output of a Pulse Generator with a 1 second period.
You may want to delay the start of popping until the buffer is full.
See my attached example
20 Comments
Tamar
on 11 Jul 2023
I can’t open your example because I have 2022.
Anyway, how can I do from one signal vector that it’s value are - [1sec, 2sec, 3sec…]?
And what if I want the push be variable? Than I can’t pop the fifo a vector because I don’t know the size that the vector should be
Tamar
on 11 Jul 2023
I have pulses. Let’s say my step time is 1 sec and all the simulation run 30 sec. My source is something that make pulses. I don’t know the time for each pulse. I would like to get the values for each pulse to Matlab function block. For example: My source is [0 0 3 6 10 6 3 0 0 5 6 10 6 5 0 6…] (every value is one step time) I want to input to Matlab function block one time [3 6 10 6 3] and the second time [5 6 10 6 5]… - do a vector and cut it according the zeros value each pulse
Jon
on 11 Jul 2023
What will your Matlab function block output while it is waiting for the first 5 non-zero values? What will it output while it waits to accumulate the next 5 non-zero values?
What is the bigger context here, do you really have to do this in Simulink, or do you have data in a file that could just be processed using MATLAB?
Tamar
on 11 Jul 2023
I wish I could do it without simulink but My data is create in simulink and I use simulink blocks also after the Matlab function block, and the time line is important.
I can triggered the Matlab function block according the value of the pulse (if the memory is above zero and the current value is zero), and then don’t use it when I don’t have enough values. I don’t really care if the Matlab function block get inputs all the time steps and output nan values while the input is nan or zero.
Jon
on 11 Jul 2023
Will the data of interest alway come as 5 consecutive non-zero values followed by some zeros, or sometimes are there zeros between the values that must be discarded until you accumulate 5 non-zero values, so is it always like your example, or could it be
[0 0 3 6 0 10 6 3 0 0 5 6 10 0 0 6 5 0 6…] (every value is one step time) input to Matlab function block one time [3 6 10 6 3] and the second time [5 6 10 6 5]?
Tamar
on 11 Jul 2023
No. Every zero value is new pulse. It’s not always five values of numbers. It’s could be [ 0 3 6 3 0 0 0 5 6 10 6 5 0 1 0 0] and the input to Matlab function block is [3 6 3] and then [5 6 10 6 5] and then [1].
I tried to use buffer block but in this block I have to set size.
Moreover, I tried to use to file block and from file block and restart the file when I want but it’s impossible to use to file block and from file block that have the same name.
Jon
on 12 Jul 2023
I think this approach may work for you. I have use a tapped delay line to buffer the values. I detect a zero following a non-zero pulse train (e.g. the zero following 3 6 3 in your example). When I detect this zero, I trigger a triggered subsystem that hold the MATLAB function block. and receives the entire contents of the FIFO (output of the tapped delay line). Inside the function block I extract just the most recent pulse train, with some additional logic, and the compute the desired function of the pulse train values.
I have attached the model demonstrating this saved as version 2022a. Hope you can open and run it Also here is a screen shot for an overview. Below is the code inside of the MATLAB function block.
Note this approach won't find a final non-zero pulse train at the end of the simulation unless the last element in the input is a zero, so you may have to be careful about that edge case.
function y = fcn(u)
% function to do demonstrate doing something
% with group of pulse inputs
% u is output of tapped delay line, which essential acts as FIFO buffer
% Function is executed at end of non-zero pulse train, since u is entire
% contents of FIFO, and non-zero pulse trains are variable length, there
% may be some previous pulse trains and/or zeros in the buffer
% Just keep the newest non-zero values
idx = find(u==0,1,'last');
% Note even though idx has only one element, it seems that Simulink doesn't
% know this and has errors unless you just use the first element of this
% one element vector, so use idx(1), rather than idx
iStart = idx(1) + 1;
x = u(iStart:end,1); % just the most recent pulse train
% Calculate something using the non-zero pulse train, I will compute the
% total, but you can insert what you want here
y = sum(x);

Jon
on 13 Jul 2023
Edited: Jon
on 13 Jul 2023
I already show a screen shot in my comment above, (do you see it now?) and attached a .png of the screenshot. Also are you sure you are trying to use the correct file? You should download pulseExas2022a.slx attached to my comment above.
Tamar
on 17 Jul 2023
It’s working, thank you!
Another question - how can I do the same to vectors? I have fixed size vector [1 14] every x second (according the pulses) and I want all the vector of 10 seconds for other Matlab function. It’s impossible to do tapped delay to vector :(
Tamar
on 17 Jul 2023
I replace your Matlab function with this function:
function y = fcn(u)
idx = find(u==0,1,'last');
iStart = idx(1) + 1;
x = u(iStart:end,1)'; % just the most recent pulse train
y = zeros(1,10);
y(1,1:length(x)) = x.*2;
end
and write it to the workspace in the triggered subsystem.
I got a time of [7,14,16,21,23,28] and the vector of eatch:
7: [2, 4, 6, 8, 10, 12, 14, 0, 0, 0]
14: [10, 12, 20, 12, 10, 0, 0, 0, 0, 0]
16: [12, 0, 0, 0, 0, 0, 0, 0, 0, 0]
21: [2, 4, 0, 0, 0, 0, 0, 0, 0, 0]...
Now I want the vectors of any 10 seconds will be in matrix:
1) [2, 4, 6, 8, 10, 12, 14, 0, 0, 0]
2) [10, 12, 20, 12, 10, 0, 0, 0, 0, 0; 12, 0, 0, 0, 0, 0, 0, 0, 0, 0]...
I don't care if the time step of the matrix be the last time in the 10 seconds (16 in case 2) or the first (14)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
