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

2 views (last 30 days)
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.

Answers (1)

Chinmayi Lanka
Chinmayi Lanka on 18 Jan 2017
Edited: Chinmayi Lanka on 18 Jan 2017
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 Comment
Lennart
Lennart on 19 Jan 2017
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?

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!