% ------------------------------
% Matlab csv数据批量预处理输出至Excel
% 时间:2022年8月28日
% 所需软件代码:Matlab R2020a
% 本代码版本:3.00
% 程序员:wishing
% ------------------------------
%% 初始化
% 清理运行环境
clearvars
clc
%% 单个文件处理
%% 目标:
% 1.筛选出每个被试4种速度条件下的数据(无应答率,正确率,反应时,最快10%平均反应时),需计算出结果,最终每个被试有四中速度下的结果,每种速度下有四个值。
% 2.整理成RW状态下speed1(+-0.5)/speed2(+-1)/speed3(+-1.5)/speed4(+-2)下的所有被试的数据。(RW下四个Sheet,SD下四个sheet,每个sheet下每个被试有四个值)
%% 循环处理
for No = 10 : 37
filename_RW = string(append('D:\desk\speed\data_1\behavior\RW','\','1',string(No),'.csv'));%文件位置,制作NO循环
filename_SD = string(append('D:\desk\speed\data_1\behavior\SD','\','2',string(No),'.csv'));%文件位置,制作NO循环
%% 数据准备
data_RW = readmatrix(filename_RW); % 导入成矩阵
data_SD = readmatrix(filename_SD);
A = data_RW(:,[2 18 19 20]);%提取2,18,19,20列组成新数组,分别代表speed,res_keys,res_corr,res_rt
B = data_SD(:,[2 18 19 20]);
R_speed_1 = A(A(:,1)==7.5|A(:,1)==8.5,:);% 筛选出speed1,2,3,4的数据
R_speed_2 = A(A(:,1)==7|A(:,1)==9,:);
R_speed_3 = A(A(:,1)==6.5|A(:,1)==9.5,:);
R_speed_4 = A(A(:,1)==6|A(:,1)==10,:);
S_speed_1 = B(B(:,1)==7.5|B(:,1)==8.5,:);
S_speed_2 = B(B(:,1)==7|B(:,1)==9,:);
S_speed_3 = B(B(:,1)==6.5|B(:,1)==9.5,:);
S_speed_4 = B(B(:,1)==6|B(:,1)==10,:);
%% 计算指标
%RW
R_speed_1_norate = numel(find(isnan(R_speed_1(:,2))))/14;%计算无应答率,nan个数/14
R_speed_1_corate = sum(R_speed_1(:,3))/14;%计算正确率,1求和/14
R_speed_1_meanrt = mean(R_speed_1(:,4),'omitnan');%计算均值,忽略nan
R_speed_1_fastrt = min(R_speed_1(:,4));%最快10%,14个中就是最小值
R_speed_2_norate = numel(find(isnan(R_speed_2(:,2))))/14;
R_speed_2_corate = sum(R_speed_2(:,3))/14;
R_speed_2_meanrt = mean(R_speed_2(:,4),'omitnan');
R_speed_2_fastrt = min(R_speed_2(:,4));
R_speed_3_norate = numel(find(isnan(R_speed_3(:,2))))/14;
R_speed_3_corate = sum(R_speed_3(:,3))/14;
R_speed_3_meanrt = mean(R_speed_3(:,4),'omitnan');
R_speed_3_fastrt = min(R_speed_3(:,4));
R_speed_4_norate = numel(find(isnan(R_speed_4(:,2))))/14;
R_speed_4_corate = sum(R_speed_4(:,3))/14;
R_speed_4_meanrt = mean(R_speed_4(:,4),'omitnan');
R_speed_4_fastrt = min(R_speed_4(:,4));
%SD
S_speed_1_norate = numel(find(isnan(S_speed_1(:,2))))/14;
S_speed_1_corate = sum(S_speed_1(:,3))/14;
S_speed_1_meanrt = mean(S_speed_1(:,4),'omitnan');
S_speed_1_fastrt = min(S_speed_1(:,4));
S_speed_2_norate = numel(find(isnan(S_speed_2(:,2))))/14;
S_speed_2_corate = sum(S_speed_2(:,3))/14;
S_speed_2_meanrt = mean(S_speed_2(:,4),'omitnan');
S_speed_2_fastrt = min(S_speed_2(:,4));
S_speed_3_norate = numel(find(isnan(S_speed_3(:,2))))/14;
S_speed_3_corate = sum(S_speed_3(:,3))/14;
S_speed_3_meanrt = mean(S_speed_3(:,4),'omitnan');
S_speed_3_fastrt = min(S_speed_3(:,4));
S_speed_4_norate = numel(find(isnan(S_speed_4(:,2))))/14;
S_speed_4_corate = sum(S_speed_4(:,3))/14;
S_speed_4_meanrt = mean(S_speed_4(:,4),'omitnan');
S_speed_4_fastrt = min(S_speed_4(:,4));
%% 输出结果
% RW
writematrix([R_speed_1_norate R_speed_1_corate R_speed_1_meanrt R_speed_1_fastrt],'RW_speed.xlsx','sheet',1,'Range',string(append('A',string(No),':D',string(No))));%循环写入表格
writematrix([R_speed_2_norate R_speed_2_corate R_speed_2_meanrt R_speed_2_fastrt],'RW_speed.xlsx','sheet',2,'Range',string(append('A',string(No),':D',string(No))));
writematrix([R_speed_3_norate R_speed_3_corate R_speed_3_meanrt R_speed_3_fastrt],'RW_speed.xlsx','sheet',3,'Range',string(append('A',string(No),':D',string(No))));
writematrix([R_speed_4_norate R_speed_4_corate R_speed_4_meanrt R_speed_4_fastrt],'RW_speed.xlsx','sheet',4,'Range',string(append('A',string(No),':D',string(No))));
% SD
writematrix([S_speed_1_norate S_speed_1_corate S_speed_1_meanrt S_speed_1_fastrt],'SD_speed.xlsx','sheet',1,'Range',string(append('A',string(No),':D',string(No))));
writematrix([S_speed_2_norate S_speed_2_corate S_speed_2_meanrt S_speed_2_fastrt],'SD_speed.xlsx','sheet',2,'Range',string(append('A',string(No),':D',string(No))));
writematrix([S_speed_3_norate S_speed_3_corate S_speed_3_meanrt S_speed_3_fastrt],'SD_speed.xlsx','sheet',3,'Range',string(append('A',string(No),':D',string(No))));
writematrix([S_speed_4_norate S_speed_4_corate S_speed_4_meanrt S_speed_4_fastrt],'SD_speed.xlsx','sheet',4,'Range',string(append('A',string(No),':D',string(No))));
end
网友评论