ITKeyword,专注技术干货聚合推荐

注册 | 登录

Matlab function to pass a vector into simulink using 'fromworkspace'

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:MATLAB Simulink embedded function variable setting

To generate embeddable code, the Embedded MATLAB Function blockrelies on an analysis that determines the size, class, and complexityof each variable.

I would like to write a matlab function that wraps around a simulink block. The function should load data into the simulink model, run it, then return the data back from the function.

The only way I can think of doing this is by using the 'To Workspace' and 'From Workspace' blocks in simulink. The problem is that the 'From Workspace' block doesn't pick up variables from the function scope, only from the workspace scope.

Below is the only solution I could come up with, which basically converts the incoming vector to a string, and then creates a function which gets called when the model starts (effectively this is just as bad as eval).

Here is the code:

function [ dataOut ] = run_simulink( dataIn )

    % Convert data to a string (this is the part I would like to avoid)
    variableInitString = sprintf('simin = %s;', mat2str(dataIn));

    % we need both the name and the filename
    modelName = 'programatic_simulink';
    modelFileName = strcat(modelName,'.slx');

    % load model (without displaying window)
    load_system(modelFileName);

    % Set the InitFcn to the god awful string
    % this is how the dataIn actually gets into the model
    set_param(modelName, 'InitFcn', variableInitString);

    % run it
    sim(modelName);

    % explicity close without saving (0) because changing InitFcn
    % counts as changing the model.  Note that set_param also
    % creates a .autosave file (which is deleted after close_system)
    close_system(modelName, 0);

    % return data from simOut that is created by simulink
    dataOut = simout;
end

And you run it like this: run_simulink([0 0.25 0.5 0.75; 1 2 3 4]') where the first part of the matrix is the time vector.

Finally here is the underlying simulink file with the workspace block properties open for completeness.

(If the image is fuzzy, click to enlarge)

Is there a more clean way to do this without the mat2str() and sprintf() ? The sprint line takes forever to run, even with vectors of size 50k.

推荐:Define the input/output port name for the Inlined C++ S-function block in Matlab Simulink

DO IT by the Mask option of the context menu of the block by right-click mouse button. and input the text like   disp('Metropolis Hastings/n(MH) sampl

matlab simulink
|
  this question
edited May 2 at 10:59 Wolfie 3,558 1 7 25 asked Feb 26 '15 at 5:37 portforwardpodcast 4,265 1 25 47

 | 

1 Answers
1

It depends which version you are using. In recent version, you can specify the workspace to use as part of the call to the sim function, e.g.:

sim(modelName,'SrcWorkspace','current'); % the default is 'base'

For more details, see the documentation on sim. In older versions (not sure exactly when this changed, some time around R0211a or R0211b I think), you had to use simset, e.g.:

 myoptions = simset('SrcWorkspace','current',...
                       'DstWorkspace','current',...
                       'ReturnWorkspaceOutputs', 'on');
 simOut = sim(mdlName, endTime, myoptions);

Update

To return data back from sim in R2014b, you need to use an output argument when calling sim, which contains all the simulation outputs, e.g.:

simOut = sim(modelName,'SrcWorkspace','current'); % the default is 'base'

simOut is a Simulink.SimulationOutput object containing the time vector, the logged states and outputs of the model.


|
  this answer
edited Feb 27 '15 at 6:59 answered Feb 26 '15 at 9:58 am304 11.1k 2 13 28      I tried the first option in R2014b and it works great! Except it only gets data into the simulink, but breaks returning data back. "SrcWorkspace" works, but throwing on "DstWorkspace" returns an error: 'DstWorkspace' parameter is not supported in this form of syntax for sim command. Any help for returning variables? –  portforwardpodcast Feb 27 '15 at 3:31      I've
 d my method by writing to a .mat file instead of using sprintf. This
 s performance greatly, but I would still like to know the "correct" way to do this. –  portforwardpodcast Feb 27 '15 at 3:32      See my updated answer, but it's all in the documenation for sim: uk.mathworks.com/help/simulink/slref/sim.html –  am304 Feb 27 '15 at 6:59

 | 

推荐:simulink仿真错误:MATLAB Function Interface Error

MATLAB Function Interface Error: Error calling generated SFunction, untitled_sfun. Invalid MEX-file ‘C 这是在搭simulink 的LDPC编译码模块遇到的一个错


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。