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.."

5 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by