Error when generating C code in MATLAB

3 ビュー (過去 30 日間)
User 864
User 864 2017 年 10 月 26 日
コメント済み: Walter Roberson 2017 年 10 月 26 日
Hello,
I'm trying to generate a C code from MATLAB with MATLAB Coder, but some errors. First, this is my function:
function feats = covFeatures(x, fs)
feats = zeros(1,3);
[c, lags] = xcorr(x);
minprom = 0.0005;
mindist_xunits = 0.3;
minpkdist = floor(mindist_xunits/(1/fs));
[pks,locs] = findpeaks(c,...
'minpeakprominence',minprom,...
'minpeakdistance',minpkdist);
tc = (1/fs)*lags;
tcl = tc(locs);
And the errors say: Error calling function. Undefined function or variable 'tc'. The first assignment to a local variable determines its class. Undefined function or variable 'tcl'. The first assignment to a local variable determines its class.
I tried defining them at the beginning as:
tc = [];
tcl=[];
But this didn't work. How can I fix this? THANKS!

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 26 日
You should be declaring the sizes of all of your variables that being assigned non-constants. That can either be by assigning zeros() of appropriate size to them, or by using coder.varsize() .
Unless you configure for dynamic memory allocation, you should also use an assert() on size() of x to enforce an upper bound on the amount of memory that needs to be allocated on the stack, which is very important when generating for HDL, and can be important when generating for processors with a limited amount of memory. If you are targetting a DSP or 16 bit microcontroller, I would recommend using assert() unless perhaps you can be quite certain that the required amount of memory will be below 1024 bytes.
  2 件のコメント
User 864
User 864 2017 年 10 月 26 日
編集済み: User 864 2017 年 10 月 26 日
I fixed it with coder.varsize() but now it show me an error saying Function call failed (I really have a big function and this one is inside it). The error appears here:
tc = (1/fs)*lags;
Walter Roberson
Walter Roberson 2017 年 10 月 26 日
What is the new version of the code?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeC Code Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by