Is it possible to write global data in a simulink model (used for code generation), from a matlab function defined outside the model?
2 次查看(过去 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.
0 个评论
回答(1 个)
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!