Error while reading/writing variables from embedded matlab function..
3 次查看(过去 30 天)
显示 更早的评论
i am writing an embedded matlab function . the code is working fine when i enter the variables manually into the code but what i want to do is to import the variables from a .mat file
i have tried loading the file using blockproperties>>callback>>initfcn>>load("filtercoeff.mat') which contains the coefficient vector 'h'
a part of my code is----> %%%%%%%%%%%%%%%%% function y = fcn(u); eml.extrinsic('load'); load('FilterCoeff.mat'); h1=h; sum=0; for j=1:11; sum=sum+u(j)*h1(j); end y = sum; %%%%%%%%%%%%%%%%% but when i try to run the simulation is says" Undefined function or variable 'h'.
Function 'Embedded MATLAB Function' (#18.532.533), line 13, column 4: "h" Launch diagnostic report."
please tell me where i am going wrong..
0 个评论
采纳的回答
Rick Rosson
2012-3-19
The initfcn callback in Block Properties is loading the coefficients into the variable h in the global MATLAB Workspace. Unfortunately, the Embedded MATLAB function does not have direct access to the global workspace.
I can think of two ways to address this issue (I am sure there are other options as well):
Option 1
First, delete the code in the initfcn callback. Then, declare the filter coefficients h inside the EML Function as a persistent variable, and initialize this variable by loading the coefficients:
eml.extrinisic('load');
persistent data
if isempty(data)
data = load('FilterCoeff.mat');
end
In this case, you will find that the coefficients are located in:
data.h
Option 2
Pass the filter coefficients in to the EML Function block as an explicit input argument. If you want, you can specify this argument as a 'parameter' instead of an 'input' using the Edit Data/Ports option in the EML Function Editor.
HTH.
Rick
3 个评论
Rick Rosson
2012-3-20
I think I made a mistake in Option 1 (I have fixed it now). But I am glad that Option 2 worked out for you. I think it is probably the better way to go anyway.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!