MATLAB筛选数据
文章目录
0.前言
任务要求如下;
把36万行的数据集筛选出来,只剩下1800多行
1.程序代码
%%
clear;
clc;
filename_Old = 'WLTC.XLS';
[~,Sheet,~]=xlsfinfo(filename_Old); % 求出文件有多少个sheet
cell_array = [] ;
process_show=waitbar(0,'检查进度'); %显示初始界面
for i= 1:length(Sheet)
[~,~,total_number] = xlsread(filename_Old,Sheet{i}); % 因为一个sheet最多是65501行
Percentage = i/length(Sheet);
waitbar(Percentage,process_show,['已完成' (num2str(round(100*Percentage))) '%']); %进度条,进度增加情况 后面有判断是否关闭
flag_5_test = 1; % 用来标志 每10个就取一次值
for VehicleSpeed_i = 1:length(total_number) % 因为一个sheet最多是65501行
if isnan(total_number{VehicleSpeed_i,4}) | strcmp(char(total_number{VehicleSpeed_i,4}) , 'ESP_VehicleSpeed[km/h]' ) % 为空则跳过
continue;
else % 表示有数据 则记录当前的行数
flag_5_test =flag_5_test + 1;
if mod(flag_5_test,10) == 0 %如果是能被10整除
try %错误判断
%% 取出 Time 值
Current_Time = round(total_number{VehicleSpeed_i,1});
%% 取出 EMS_EngineSpeed 值
EMS_EngineSpeed = round(total_number{VehicleSpeed_i-1,2});
%% 取出 EMS_IndicatedRealEngTorq 值
EMS_IndicatedRealEngTorq = round(total_number{VehicleSpeed_i+1,3});
%% 取出 ESP_VehicleSpeed 值
ESP_VehicleSpeed =round (total_number{VehicleSpeed_i,4});
catch
EMS_EngineSpeed = round(total_number{VehicleSpeed_i-1,2});
%% 取出 EMS_IndicatedRealEngTorq 值
EMS_IndicatedRealEngTorq = round(total_number{VehicleSpeed_i-2,4});
end
%% 把读取的4个数据依次写入新表中
Data_need = [ Current_Time ,EMS_EngineSpeed ,EMS_IndicatedRealEngTorq ,ESP_VehicleSpeed ];
cell_array = [cell_array;Data_need]; % 按照列来存储
% cell_array = [cell_array,Data_need]; % 按照行来存储
end
end
end
end
close(process_show);% 循环结束,关闭进度条
disp('正在保存数据...')
save cell_array
disp('保存成功')
cell_array=load('cell_array.mat'); %a是一个的mat数据
X_time = cell_array.cell_array(:,1);
y1_EMS_EngineSpeed = cell_array.cell_array(:,2);
y2_IndicatedRealEngTorq = cell_array.cell_array(:,3);
y3_VehicleSpeed = cell_array.cell_array(:,4);
plot(X_time,y1_EMS_EngineSpeed,X_time,y2_IndicatedRealEngTorq,X_time,y3_VehicleSpeed)