Insert elements into a 4D array

6 views (last 30 days)
Hello,
I have a 4-dimensional buffer called:
frameBuffer = zeros (width, height, numChannels, framesPerSymbol);
in which I want to insert 3-dimensional video frames (width, height, numChannels). The variable framesPerSymbol is the number of frames that will go in the buffer, that is, if framesPerSymbol is 10, my buffer will be 10.
And this is where I run it:
frameBuffer = zeros(width, height, numChannels,framesPerSymbol);
framesInBuffer = 0;
while hasFrame(videoObject)
frame = readFrame(videoObject);
shiftBuffer(frameBuffer,frame); % <--------- THIS IS THE PART
% We update the framesInBuffer counter
framesInBuffer = framesInBuffer + 1;
if framesInBuffer > framesPerSymbol
framesInBuffer = framesPerSymbol;
bypassEncoding = false;
else
bypassEncoding = true;
end
end
The way I was doing the insert was like this, but I don't get results:
function [] = shiftBuffer (frameBuffer,frame)
N = size(frameBuffer,4); % Buffer size
for i = 1:N
frameBuffer(i) = frame;
end

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 12 Apr 2020
There are 2 mistakes in the above implementation:
  1. Accessing a frame of frameBuffer as frameBuffer(i) instead of frameBuffer(:,:,:,i)
  2. The function shiftBuffer changes the values of frameBuffer locally in the function and in the main code the frameBuffer isn't changed.
I suggest you to make the following changes.
Change the function shiftBuffer as follows:
function frameBuffer = shiftBuffer(frameBuffer,frame,framesInBuffer)
frameBuffer(:,:,:,,framesInBuffer) = frame;
end
and the following lines in your code
frame = readFrame(videoObject);
shiftBuffer(frameBuffer,frame); % <--------- THIS IS THE PART
% We update the framesInBuffer counter
framesInBuffer = framesInBuffer + 1;
to
frame = readFrame(videoObject);
% We update the framesInBuffer counter
framesInBuffer = framesInBuffer + 1;
frameBuffer = shiftBuffer(frameBuffer,frame,framesInBuffer);

More Answers (0)

Categories

Find more on Simulink 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!