我想编写一个包含simulink块的matlab函数 . 该函数应将数据加载到simulink模型中,运行它,然后从函数返回数据 .
我能想到的唯一方法就是在simulink中使用'To Workspace'和'From Workspace'块 . 问题是'From Workspace'块不从功能范围中获取变量,只从工作空间范围中获取变量 .
下面是我能想到的唯一解决方案,它基本上将传入的向量转换为字符串,然后创建一个在模型启动时被调用的函数(实际上这与eval一样糟糕) .
这是代码:
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, &