simulink通过代码运行,输入数据从excel读入,输出数据写入excel,并进行数据误差判断
clc,clear;
%从excel读入数据
mark=xlsread('rawdata.xlsx');
%定义simulink的全局变量
Interia=0.05;
Rs=0.2861;
Vbus=350;
%创建数组为输出变量每个步长的结果
IsEst_result=[];
SpeedEst_result=[];
TqEst_result=[];
%用来存放最后15个步长的值
IsEst_result1=zeros(15,1);
SpeedEst_result1=zeros(15,1);
TqEst_result1=zeros(15,1);
%得到数据总长度,之后进行数据索引= mark(2,2);%得到第一行第一列数据
num = length(mark);
% 读取行数r、列数c
[r,c] = size(mark);
%用来依次写入excel的迭代数值
k=1;
%表示误差是大于20%还是小于%20,为cell字符
pass = {'pass'};
fail = {'fail'};
%循环读excel的每一行数据
for i=1:r
% i=4
Is=mark(i,1);
Phase=mark(i,2);
Speed=mark(i,3);
Tq=mark(i,4);
% Torque Ripple=mark(i,5);
Voltage=mark(i,6);
% Fundamental Voltage=mark(i,7);
Vd=mark(i,8);
Vq=mark(i,9);
% Power Factor=mark(i,10);
% Total Loss=mark(i,11);
% Iron Loss=mark(i,12);
% Hysteresis Loss (Iron Loss)=mark(i,13);
% Eddy Current Loss (Iron loss)=mark(i,14);
% Copper Loss=mark(i,15);
% d-Axis Flux=mark(i,16);
% q-Axis Flux=mark(r,17);
Ld=mark(i,18);
Lq=mark(i,19);
% Magnet Flux(d-axis)=mark(i,20);
% Magnet Flux(q-axis)=mark(i,21);
% Phase Offset=mark(i,22);
% Shaft Torque=mark(i,23);
% end
%暂停0.5秒
% pause(0.5);
%% *********************************************************************************
%运行仿真
simOut = sim('model2.slx');
%% 1、*********************************************************************************
%将示波器的结果放到数组中,总共为(仿真时间/步长)个数据,切记,IsEst这个名字:示波器设置中随时间变化勾选并命名IsEst
IsEst_result=IsEst.signals.values;
%将26-40的结果放入数组
for j=26:40
IsEst_result1(:,1)=IsEst_result(j,1);
end
%求该数组的均值
IsEst_mean=mean(IsEst_result1);%求出均值
%判断每一行每个数据的误差,并依次写入excel
if((IsEst_mean-Is)/IsEst_mean<0.2)
xlswrite('str.xlsx',pass,'A',num2str(k));
else
xlswrite('str.xlsx',fail,'A',num2str(k));
end
%% 2、*********************************************************************************
SpeedEst_result=SpeedEst.signals.values;
for j=26:40
SpeedEst_result1(:,1)=SpeedEst_result(j,1);
end
SpeedEst_mean=mean(SpeedEst_result1);
if((SpeedEst_mean-Speed)/SpeedEst_mean<0.2)
xlswrite('str.xlsx',pass,'B',num2str(k));
else
xlswrite('str.xlsx',fail,'B',num2str(k));
end
%% 3、*********************************************************************************
TqEst_result=TqEst.signals.values;
for j=26:40
TqEst_result1(:,1)=TqEst_result(j,1);
end
TqEst_mean=mean(TqEst_result1);
if((TqEst_mean-Tq)/TqEst_mean<0.2)
xlswrite('str.xlsx',pass,'C',num2str(k));
else
xlswrite('str.xlsx',fail,'C',num2str(k));
end
%% *********************************************************************************
k=k+1;
end
%将str.xls的数据合并
[~,~,DataCell_Is]=xlsread('str.xlsx','A');
[~,~,DataCell_Speed]=xlsread('str.xlsx','B');
[~,~,DataCell_Tq]=xlsread('str.xlsx','C');
%创建表头名称
A = {'IsEst_result'};
B = {'SpeedEst_result'};
C = {'TqEst_result'};
%合并excel数据
xlswrite('rawdata.xlsx', A,'X1:X1');
xlswrite('rawdata.xlsx', B,'Y1:Y1');
xlswrite('rawdata.xlsx', C,'Z1:Z1');
%写入表头
xlswrite('rawdata.xlsx',DataCell_Is,'sheet1','X2');
xlswrite('rawdata.xlsx',DataCell_Speed,'sheet1','Y2');
xlswrite('rawdata.xlsx',DataCell_Tq,'sheet1','Z2');