How do I resolve the error "The signal at output port 2 of 'ftranssine2freq/MATLAB Function/ SFunction ' is a variable-size signal with a nondiscrete sample time.."

조회 수: 2 (최근 30일)
Hello everyone I was trying some simple code to find fourier transform of two cosines of different frequency numerically i.e., without using the function fft() as my guide has advised me to calculate it numerically for some specific purpose. after running the codes I am getting following error: The signal at output port 2 of 'ftranssine2freq/MATLAB Function/ SFunction ' is a variable-size signal with a nondiscrete sample time.
function y = fcn(z)
%#codegen
y=z;
N=250;
ts=.002;
u=[0:N-1]*ts;
k=0;
for f=0:1:800
k=k+1;
y(k)=real(trapz(u,z.*exp(-1i*2*pi*f*u)));
end
plot(f, abs(y))

답변 (1개)

Abhinav Aravindan
Abhinav Aravindan 2025년 1월 2일
편집: Abhinav Aravindan 2025년 1월 2일
The error you are facing is likely because the “MATLAB Function” block sample time has been set to the default value of “-1”. You may try setting the sample time as per your requirement to resolve the error.
Also, it appears that you are trying to dynamically change the size of “y” in the loop. The variable “k” in the loop is used to index “y”, but if "k" exceeds the initial length of “y”, you may encounter an indexing error. Hence, it is recommended to preallocate “y” to the required size.
Please refer to the below link wherein a similar query has been addressed:

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by