【研究工具】Matlab批量数据处理

学习的实验往往会记录大量的原始数据,excel处理起来很不方便,好在都是格式化的,用matlab编写了一个方法,方便自己在不同场景套用。

读写格式化的数据文件(用数据类型table的方法)

% data analysis: 6 contexts;seperation data from different contexts
function contextSeperation(subName)
    % subName = '6001_jushanzhong';

    %curFile = fopen([subName,'.txt'],'r');
    matData = readtable([subName,'.txt']);

    % correct error in former recording
    errData = matData.Error;
    for i = 1 : length(errData)
        if errData(i) < - 180
            errData(i) = errData(i) + 360;
        end
    end
    matData.correctedErr = errData;

    % add new row 'rotation'
    matData.bias = matData.RealTarget-matData.ShowTarget;

    % add new row 'subjectName'
    f = @(x) subName;
    cellName = cell(length(errData),1);
    cellName = cellfun(f,cellName,'UniformOutput',false);
    matData.subName = cellName;

    arrCenter = matData.CenterInd;
    uniqCenter = unique(arrCenter);
    numCenter = length(uniqCenter);

    newTrialList = [1:10 1:40 1:10]';

    % 按序号分出来
    for k = 1 : numCenter
        tmpInd = find(arrCenter == uniqCenter(k));
        tmpArr = matData(tmpInd,:);
        tmpArr.Trial = newTrialList;
        writetable(tmpArr,['seperate\',subName,'_',num2str(k),'.txt'],'Delimiter','\t');
    end
end

批处理文件夹中所有同类文件

% 读取目录下所有的文件信息
fileList = dir(cd);
listLength = length(fileList);

for i = 1 : listLength
    if ~fileList(i).isdir
        full_name = [cd,'\',fileList(i).name];        
        [pathstr,name,ext] = fileparts(full_name); % 获取需要处理的文件文件名和拓展名
        if strcmp(ext,'.txt')
            contextSeperation(name);
        end
    end
end

合并文件夹中所有数据文件

之前数据量小的文件一直都用的批处理.bat来合并,最近数据量大了这个功能总是出问题,还是自己写一个比较放心。

fileList = dir(cd);
listLength = length(fileList);

C = [];

for i = 1 : listLength
    if ~fileList(i).isdir
        full_name = [cd,'\',fileList(i).name];        
        [pathstr,name,ext] = fileparts(full_name); % 获取需要处理的文件文件名和拓展名
        if strcmp(ext,'.txt') % 要拼合的文件类型
            tmpTable = readtable(full_name);
            if isempty(C)
                C = tmpTable;
            else
                C = [C; tmpTable];
            end
            
        end
    end
end
writetable(C,'STS_all.txt','Delimiter','\t');

(新增)合并在一个文件的写法


subDir = 'procedure';
fileList = dir([cd, '\',subDir]);
listLength = length(fileList);

for i = 1 : listLength
    if ~fileList(i).isdir
        full_name = [cd, '\',subDir,'\',fileList(i).name];        
        [pathstr,name,ext] = fileparts(full_name); % 获取需要处理的文件文件名和拓展名
        if strcmp(ext,'.txt')    
            funcReProTable(subDir,name);            
        end
    end
end


function funcReProTable(subDir,name)

    matData = readtable([subDir,'\',name,'.txt'],'Format','%s%s%u%s%s%u%u%f%f%s%s%u%f%f%s%s%s%s%s%s%s','Delimiter','\t');
    
    splitname = strsplit(name,'_');


    % remove useless column
    matData = removevars(matData,{'DisplayCat','isCatchTrial','CurBlockCorrect','CurBlockCatchTrialCorrect','BlocktotAcc','BlockCatchTrialtotAcc','Var20','Var21'});
    
    % add new row 'CatCond'
    CatCond = cell(length(matData.SubName),1);
    CatCond(:) = {splitname(2)};
    matData = addvars(matData,CatCond,'Before','CurBlock');

           
    % sep test from learning
    testInd = [find(strcmp(matData.CurBlock,'oldStim')) find(strcmp(matData.CurBlock,'normStim'))];
    testData = matData(testInd,:);
    testData.CurBlock(:) = {'test'};
    testData = removevars(testData,{'RT02'});
    testData = renamevars(testData,"RT01","RT");
    
    learnInd = 1 : testInd(1,1)-1;
    learnData = matData(learnInd,:);
    learnData = removevars(learnData,{'RT01'});
    learnData = renamevars(learnData,"RT02","RT");
    
    % procedureFile
    proTable = [learnData; testData];
    
    

    writetable(proTable,['data\procedure\limitPT_procedure_',proTable.CatCond{1}{1},'_',proTable.ExpCond{1},'_',proTable.SubName{1},'_old.txt'],'Delimiter','\t');

end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容