Error while reading/writing variables from embedded matlab function..

1 次查看(过去 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..

采纳的回答

Rick Rosson
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
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 CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by