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

1 view (last 30 days)
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..

Accepted Answer

Rick Rosson
Rick Rosson on 19 Mar 2012
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 Comments
Rick Rosson
Rick Rosson on 20 Mar 2012
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!