It will occur error when Function sparse is used in simulink model

12 ビュー (過去 30 日間)
alamo
alamo 2011 年 4 月 11 日
回答済み: monika sharma 2018 年 7 月 28 日
Hello everyone,
The function sparse is used in simulink model whose name is Embedded MATLAB Function. Embedded MATLAB Function occurs error when it is running and error message is "Undefined function or variable 'sparse'."
About mel-spaced filterbank I have a function which is ok when it is running on matlab,but it will occur error when be used in simulink model as the above-mentioned error.
Matlab version is v7.11.0(R2010b)
code is following:
function x=melbankm(p,n,fs,fl,fh)
f0=700/fs;
fn2=floor(n/2);
lr=log((f0+fh)/(f0+fl))/(p+1);
% convert to fft bin numbers with 0 for DC term
bl=n*((f0+fl)*exp([0 1 p p+1]*lr)-f0);
b2=ceil(bl(2));
b3=floor(bl(3));
b1=floor(bl(1))+1;
b4=min(fn2,ceil(bl(4)))-1;
pf=log((f0+(b1:b4)/n)/(f0+fl))/lr;
fp=floor(pf);
pm=pf-fp;
k2=b2-b1+1;
k3=b3-b1+1;
k4=b4-b1+1;
r=[fp(k2:k4) 1+fp(1:k3)];
c=[k2:k4 1:k3];
v=2*[1-pm(k2:k4) pm(1:k3)];
mn=b1+1;
v=1-0.92/1.08*cos(v*pi/2);
x=sparse(r,c+mn-1,v,p,1+fn2);

採用された回答

Mike Hosea
Mike Hosea 2011 年 4 月 11 日
Indeed, SPARSE is not supported, and if you declare it extrinsic, the result will be a MATLAB type (mxArray), which you cannot do much with in an Embedded MATLAB block. More to the point, unless this is expected and one knows what one is doing, it will probably create more problems. If the sparse matrix computations are self-contained and the final output is not sparse, one is probably better off running ones own MATLAB function as an extrinsic function. So, for example if the final output is a double of the same size as the first input, I might write
function y = EmbeddedMatlabFun(x,a,b,c,d)
eml.extrinsic('myfun');
y = zeros(size(x)); % declares the output type for myfun.
y = myfun(x,a,b,c,d);
Where myfun is a .m file somewhere on the path (probably in the same directory)
function y = myfun(x,a,b,c,d)
... code ...
t = melbankm(p,n,fs,fl,fh);
... more code ...
% Here foo takes a sparse input t and returns a
% non-sparse output with the same size as x.
y = foo(x,t);
-- Mike
  1 件のコメント
alamo
alamo 2011 年 4 月 14 日
Many thanks. Your answers is very helpful to me.

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

その他の回答 (2 件)

Arnaud Miege
Arnaud Miege 2011 年 4 月 11 日
As far as I can tell, sparse is not part of the Embedded MATLAB Subset, as least in R2010b. You therefore need to declare it as an extrinsic function before you can use it:
eml.extrinsic('sparse')
If that doesn't work, you may want to post exactly what error message you're getting.
HTH,
Arnaud
  1 件のコメント
alamo
alamo 2011 年 4 月 14 日
Thanks for your kindly reply and your reply is very helpful to me.

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


monika  sharma
monika sharma 2018 年 7 月 28 日
i used eml.extrinsic function and trying to store the output of sparse code in .mat form. But when i am calling my sparse code using eml.extrinsic function, it is not saving those output result in .mat format. Please help me out to figure out this problem. How we can save results of sparse code in workspace while calling sparse code using eml.extrinsic function?

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by