0

我尝试创建一个可以运行 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 模型

我从 simulink 获取结果或设置常量 K.in1 没有问题

4

1 回答 1

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时间向量和数据向量的数据。

于 2012-11-13T14:31:08.957 回答