登录论坛

查看完整版本 : 在Matlab中合并和处理文件


poster
2019-12-10, 20:41
有没有一种方法可以在一个文件夹中运行一个循环,一个月处理30个文件,并给出每列的平均值,最大值,然后在一张excel工作表中写出来?

我有30个大小为[43200 x 30]的文件
我运行了一个不同的matlab脚本来生成它们,因此名称很容易File_2010_04_01.xls,File_2010_04_02.xls .....等等,我无法合并它们,因为它们都是20mbs,因此matlab会崩溃。



回答:

您首先可以使用函数DIR (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dir.html)获取文件列表。这是一个例子:

dirData = dir('File_2010_04_*.xls'); %# Match file names with a wildcard dataFiles = {dirData.name}; %# Get the file names in a cell array 拥有这些文件后,您可以使用XLSREAD (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.html)遍历它们以加载数据。注意XLSREAD (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.html)可以在Excel文件中返回不同版本的数据:

[numData,txtData,rawData] = xlsread(fileName); %# Where fileName is a string 在这里, numData包含文件中具有数字数据的单元格, txtData包含文件中具有文本数据的单元格,而rawData是包含文件中所有数据的单元格数组。您将必须确定要使用哪个数据数组以及如何对其进行索引,才能处理43200×30的数据矩阵。

综上所述,这是一个代码示例,说明如何处理数据以获取所有文件的列最大值和列平均值:

columnTotal = zeroes(1,30); %# Initialize column sum columnMax = -inf(1,30); %# Initialize column maxima dirData = dir('File_2010_04_*.xls'); %# Match file names in the current folder dataFiles = {dirData.name}; %# Get the file names in a cell array nFiles = numel(dataFiles); %# Number of files for iFile = 1:nFiles %# Loop over the files numData = xlsread(dataFiles{iFile}); %# Load the data %# Here, I'm assuming "numData" contains your 43200-by-30 matrix columnTotal = columnTotal+sum(numData); %# Add up column data columnMax = max(columnMax,max(numData)); %# Get the column maxima end columnAverage = columnTotal./(nFiles*43200); %# Average across all files

更多&回答... (https://stackoverflow.com/questions/2725252)