我尝试创建一个可以运行simulink模型的matlab文件。
但是将数据从MATLAB发送到simulink模型有问题。
在模型中我有Import Block
我希望从matlab发送我的数据流。
我当前的matlab代码是。
function result = CallSimulinkModel(modelName, timeStep, inputMatix, key)
if(timeStep<=0)
error('MATLAB:CallSimulinkModel','Timestep is lower or equal with 0');
end
endTime = max(size(inputMatix))*timeStep;
result = SubCallSimulinkModel(modelName, 0:timeStep:endTime, inputMatix, key);
%OutVector.const = inConst;
end
function [result,constants] = SubCallSimulinkModel(var_model,time_var,inputMatix, K)
var_setting = simset(...
'SrcWorkspace','current'...
);
% How to feed the sim model with the data from input matrix as Inport
% 1, Inport 2, ...?
[~,~,result] = sim(var_model, time_var, var_setting);
constants = K;
end
模型为
从simulink获取结果或设置常量K.in1
我没有问题答案 0 :(得分:0)
找到了一个有效的解决方案。
% For MATLAB R2012a
function [result,time] = RunSimulinkModel(modelName, timeStep, inputMatix, key)
if(timeStep<=0)
error('MATLAB:RunSimulinkModel','Timestep is lower or equal with 0');
end
endTime = max(size(inputMatix))*timeStep-timeStep;
%CCC = [time_var;D]'
% Is a 2 or more column that the first column is a time vector that
% hold all time and the other columns hold the data.
dataMatrix = [0:timeStep:endTime;inputMatix]';
[result,time] = RunSimulinkModelSub(modelName, timeStep,dataMatrix,key);
end
function [result,time] = RunSimulinkModelSub(var_model,var_steptime, D,K) %#ok<INUSD>
var_setting = struct(...
'SrcWorkspace','current',...
'SaveTime','on','TimeSaveName','tout', ...
'SaveOutput','on','OutputSaveName','yout', ...
'LoadExternalInput', 'on', 'ExternalInput', 'D',...
'FixedStep', 'var_steptime' ...
);
var_row_result = sim(var_model, var_setting);
result = var_row_result.get('yout');
time = var_row_result.get('tout');
end
'LoadExternalInput', 'on', 'ExternalInput', 'D',...
会告诉模型使用
来自D
的数据作为时间向量和数据向量。