필터 지우기
필터 지우기

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