将多个文本文件导入到Matlab中以分析数据 [英] Import multiple text files onto Matlab to analyze the data

查看:96
本文介绍了将多个文本文件导入到Matlab中以分析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是matlab的新手.因此,我正在尝试学习基础知识.我有8个tsv文件,其名称类似于2004.07.01.0000.tsv,2004.07.01.0300.tsv,其中每个文件都有72行和144列.我试图自动将所有这些文件以矩阵形式导入到matlab,以计算均值,中位数,偏度(用于数据校正).我所做的是使用matlab gui导入了一个文件(2004.07.01.0000.tsv),然后生成了一个名为importfile的函数.我正在尝试使用for循环来访问那些文件中的所有数据,但我无法弄清楚.我尝试过(完全不确定):

I am really new in matlab. So i am trying to learn the very basics. I have 8 tsv files with names like 2004.07.01.0000.tsv, 2004.07.01.0300.tsv, where each file has 72 rows and 144 columns. I am trying to automatically import all of those files to matlab in a matrix form to calculate the mean, median, skewness (for data correction). What I did is that I imported one file (2004.07.01.0000.tsv) using matlab gui, then I generated a function called importfile. I am trying to use a for loop to access all the data in those files but I could not figure it out. I tried (not sure at all):

for fileNum=1:8;
startRow=1;
endRow=72;
filename
a=importfile(filename, startRow, endRow);
end

推荐答案

如果您的importfile()函数正常工作,则以此方式在每次for-loop迭代中,您都会用最新导入的文件覆盖a.您应该串联所有文件(即矩阵).

If your importfile() function works correctly, in this manner at every for-loop iteration you'll overwrite a with the most recent imported file. You should concatenate all your files (i.e. matrices) instead.

矩阵级联可以通过行(即水平级联)或按列(即垂直级联)完成.据我了解,您需要垂直级联以生成具有144列和与单个文件包含的行数一样多的唯一矩阵.

A matrix concatenation can either be done by rows (i.e. horizontal concatenation) or by columns (i.e. vertical concatenation). As I understand, you want a vertical concatenation in order to generate a unique matrix with 144 columns and as many rows as your single files contain.

因此,您应按以下步骤更改循环

Thus you should change the loop as follows

myMatrix=[];
for fileNum=1:8;
    startRow=1;
    endRow=72;
    filename
    myMatrix=[myMatrix ; importfile(filename, startRow, endRow)];
end

垂直连接可以通过;运算符完成,因此类似A=[B ; C]的指令将通过连接矩阵BC来创建矩阵A.在您的情况下,将myMatrix初始化为空,然后将您以.tsv文件形式垂直组合(以迭代方式)importfile()的所有输出.

The vertical concatenation can be done by means of the ; operator, thus an instruction like A=[B ; C] will create a matrix A by concatenating matrices B and C. In your case you initialize myMatrix as empty and then you will vertically concatenate (in an iterative fashion) all outputs from importfile(), that are your .tsv files.

在循环结束时,myMatrix的大小应为NxM,其中M为144,而N是所有文件中行数的总和(8 * 72).

At the end of the loop, myMatrix should have size NxM where M is 144 and N is the sum of the number of rows across all your files (8*72).

更新
如果必须将文件名显式传递给importfile()函数,则可以创建一个字符串单元格数组,其中该单元格的每个元素都是一个文件名.因此,在本例中,单元格数组将类似于:

Update
If you have to pass explicitly the filename to the importfile() function you can create a cell array of strings in which each element of the cell is a filename. Thus in our case the cell array will be something like:

filenames={'filename1.tsv','filename2.tsv',...,'filename8.tsv'};

很明显,您必须用正确的文件名替换单元格中的字符串,最后您可以按如下所示稍微编辑循环

obviously you must replace the strings inside the cell with the proper filenames and finally you can slightly edit the loop as follows

myMatrix=[];
for fileNum=1:8;
    startRow=1;
    endRow=72;
    myMatrix=[myMatrix ; importfile(filenames{i}, startRow, endRow)];
end

通过这种方式,在每次循环迭代时,第i个文件名将作为importfile()的输入,并希望将其加载.

In this manner, at every loop iteration the i-th filename will be given as input to importfile() and hopefully it'll be loaded.

为此,您应该(让事情变得简单)

For this to work you should (let's make things simple)

  1. 将Matlab脚本和函数importfile()显然放置在包含.tsv文件的同一文件夹中
  2. 将所述文件夹设置为当前文件夹
  1. place your Matlab script and obviously the function importfile() in the same folder containing your .tsv files
  2. set said folder as the Current Folder

或如果给定文件夹中有.tsv文件,而脚本在另一个文件夹中,则当前文件夹一定是包含脚本的文件夹,并且单元格数组filenames中的文件名必须包含完整路径,而不仅仅是正确的文件名.

or if you have the .tsv files in a given folder and your scripts in another folder, then the Current Folder must certainly will be the folder containing your scripts and the filenames inside the cell array filenames must contain the entire path, not just the proper filenames.

这篇关于将多个文本文件导入到Matlab中以分析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆