通过脚本运行Simulink模型
在MATLAB中,通过脚本启动simulink模型。将测试用例写入excel表格中,通过脚本读取测试用例的输入与期望输出,自动运行simulink模型,将模型输出结果与期望结果进行比较,输出测试结果,将测试结果写入excel,从而实现模型的功能测试。
测试模型如下:
测试用例如下:
对模型进行一些设置,将模型输入与workspace中的数据关联起来:
编写脚本如下:
clc
clear
% model setting
[num, txt] = xlsread('testcase.xlsx');
input1 = num(1:end,2);
input2 = num(1:end,3);
% auto script
step_size = 0.01;
stop_time = step_size*(size(input1,1) - 1);
t = [0:step_size:stop_time]';
InportInfo=find_system('test01','FindAll','On','SearchDepth',1,'BlockType','Inport');
Name_Inports = get(InportInfo,'Name');
OutportInfo = find_system('test01','SearchDepth',1,'BlockType','Outport');
InportDimen = size(InportInfo,1);
OutportDimen = size(OutportInfo,1);
u(:,1) = input1;
u(:,2) = input2;
simout = sim('test01', 'SolverType', 'Fixed-step',...
'Solver', 'ode4',...
'FixedStep', num2str(step_size),...
'StopTime', num2str(stop_time),...
'LoadExternalInput','on');
stairs(get(simout,'tout'),simout.yout{1}.Values.Data)
for i = 1:size(input1,1)
ResultJudge(i) = (num(i,4)== simout.yout{1}.Values.Data(i));
if ResultJudge(i) == 1
ResultOutput(i) = "TRUE";
else
ResultOutput(i) = "FALSE";
end
end
xlswrite('testcase.xlsx',ResultOutput','Sheet1','E2');
运行脚本,结果如下:
|