Is it possible to write global data in a simulink model (used for code generation), from a matlab function defined outside the model?

조회 수: 1 (최근 30일)
Hi all,
I have a simulink model which is used for code generation. I'm using Matlab R2010a. I use a lot of Embedded matlab function blocks, which besides having some main functionallity also write to a Simulink.Signal object defined in the model worskpace (used as global data for the model). The functional part is different for each of the many embedded matlab function blocks, but the part that writes to the Simulink.Signal object is the same. Lets use the following simple function as an example to illustrate the problem:
function y = fcn(u1,u2)
%#eml
global A
y=u1+u2; %functional part
A=y; %Write to Simulink.Signal object
So A is the Simulink.Signal object defined in the modelworkspace. Now my goal is to write A with some user defined function without having to call global A and importing it via the Ports and Data Manager. Something like this:
function y = fcn(u1,u2)
%#eml
y=u1+u2; %functional part
My_Write_Function('A',y); %Write to Simulink.Signal object
Anyone knows a way to do this? I know I can define a function in the same directory as the model and use it within an embedded matlab function block, but I'm not able to write A from there.

답변 (1개)

Chinmayi Lanka
Chinmayi Lanka 2017년 1월 18일
편집: Chinmayi Lanka 2017년 1월 18일
In releases R2011a and beyond, you will have to first declare the extrinsic function before using them in the MATLAB Function block: https://www.mathworks.com/help/simulink/slref/coder.extrinsic.html
function y = fcn(u1,u2)
%#eml
coder.extrinsic('My_Write_Function');
y=u1+u2; %functional part
My_Write_Function('A',y);
In your My_Write_function, you will have to use the 'assignin' function to assign the value A to the base workspace: https://www.mathworks.com/help/matlab/ref/assignin.html
function My_Write_Function(x,val)
assignin('base',x,val);
Now the variable A will be written to the base workspace and will be available to be read from other blocks in the model.
  댓글 수: 1
Lennart
Lennart 2017년 1월 19일
Hey Chinmayi Lanka,
Thanks for your response! As far as I know, declaring a function as extrinsic exludes it from the code generation process and lets it be executed by matlab. Since I don't want to simulate my model but generate code from it, I cannot use extrinsic functions. Any other ideas?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by