Problem with variable size

조회 수: 3 (최근 30일)
Catteau Ophélie
Catteau Ophélie 2021년 3월 24일
댓글: Walter Roberson 2021년 3월 24일
Hello, I am trying to do a customize buffer but it says that my output has a variable size and I don't wont it.
Can someone help me to resolve it ? I put my code below ( u is a vector of 8 double and FFTLength is a size of the buffer)
function data = custom_buffer( FFTLength, u)
%#codegen
persistent buffer next_index
% Define initial values
if isempty(buffer)
buffer = zeros(FFTLength,1);
next_index = 1;
end
% Populate the buffer
for k = 1 :8
buffer(next_index) = u(k);
end
% Increment the location to write to at the next time
if next_index < FFTLength
next_index = next_index + 9;
else
next_index = 1;
end
data = buffer;
end
  댓글 수: 4
Catteau Ophélie
Catteau Ophélie 2021년 3월 24일
OK, thank you I forgot this, I change it but it doesn't solve my problem about the variable size of data.
function data = custom_buffer( FFTLength, u)
%#codegen
persistent buffer next_index
% Define initial values
if isempty(buffer)
buffer = zeros(FFTLength,1);
next_index = 1;
end
% Populate the buffer
for k = 1 :8
buffer(next_index) = u(k);
next_index = next_index +1;
end
% Increment the location to write to at the next time
if next_index < FFTLength
next_index = next_index + 1;
else
next_index = 1;
end
data = buffer;
end
Walter Roberson
Walter Roberson 2021년 3월 24일
No, that way of coding can expand the array.
Suppose that we have reached next_index = FFTLength - 1. Then buffer(next_index)=u(1) puts u(1) into second last location, increment next_index to FFTLength - 0. u(2) is put there, increment next_index to FFTLength + 1. u(3) is put there... Ooops, we expanded the array!

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 24일
function data = custom_buffer( FFTLength, u)
%#codegen
persistent buffer next_index
% Define initial values
if isempty(buffer)
buffer = zeros(FFTLength,1);
next_index = 1;
end
% Populate the buffer
for k = 1 : 8
buffer(next_index) = u(k);
next_index = mod(next_index, FFTLength) + 1;
end
data = buffer;
end
  댓글 수: 2
Catteau Ophélie
Catteau Ophélie 2021년 3월 24일
I corrected like to write but there is always the variable size 's issue...
Walter Roberson
Walter Roberson 2021년 3월 24일
Is your FFTLength input defined as a Constant block? If there is any chance that the value could change between function calls, then the block is variable length.
The typical work-around for this is to define a maximum length using coder.varsize() or to
buffer = zeros(maximum_FFTLength,1);
and
assert(FFTLength <= maximum_FFTLength)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Execution Speed에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by