简体   繁体   English

将多个.csv 文件读取到 Matlab 中,同时将它们组织成新矩阵

[英]Reading multiple .csv files into Matlab while organizing them into new matrices

I have lots of.csv files that I want to read into MATLAB, but doing some organization along the way我有很多.csv 文件,我想读入 MATLAB,但在此过程中做一些整理

My first problem is my data looks like this:我的第一个问题是我的数据如下所示:

[...

file1  
ex1  
6;0  
8;0  
9;1  
file1  
ex2  
7;0  
8;1  
3;2  
file1  
ex3  
7;0  
8;1  
3;2  

The import wizard on MATLAB for some reason only takes the first header text then the data set below that and throws away everything when it reaches the next text header. MATLAB 上的导入向导出于某种原因只获取第一个 header 文本,然后是下面的数据集,并在到达下一个文本 header 时丢弃所有内容。 So how can I organize the file so that it looks like this instead?那么如何组织文件使其看起来像这样呢?

[...

file1......file1.....file1  
ex1.......ex2.......ex3  
6;0.......7;0.......7;0  
8;0.......8;1.......8;1  
9;1.......3;2.......3;2  

NOTE: the number of rows for the different ex's is always different, so you can't just spilt the file into regular chunks.注意:不同前任的行数总是不同的,所以你不能只是将文件溢出到常规块中。

My second problem is then to compare the same experiments from different files.然后我的第二个问题是比较来自不同文件的相同实验。 So I want to take the columns below "ex1" from all the different files and line then up horizontally against each other in a new matrix.所以我想从所有不同的文件中取出“ex1”下面的列,然后在一个新的矩阵中水平排列。 So that it looks like this:所以它看起来像这样:

file1.....file2.....file3.....    
ex1.......ex1.......ex1.......    
6;0.......6;0.......6;0.......  
8;0.......8;0.......8;0.......  
9;1.......9;1.......9;1....... 

NOTE: the ex's in the different files are in different orders.注意:不同文件中的 ex 的顺序不同。 I need to match up ex's within files based on matching one of the lines of header (eg whenever it is called 'track1').我需要根据匹配 header 的其中一行(例如,只要它被称为“track1”)来匹配文件中的 ex。


EDIT:编辑:

This is how the actual data looks like.这就是实际数据的样子。

Since the number of rows in each ex is different, you must use a cell matrix.由于每个 ex 中的行数不同,因此必须使用单元矩阵。

file = 'file1.csv';
h = 2; % # header lines
num_ex = 3;
r = h; % track the current row in the file
data = cell(1,num_ex);
for i=1:num_ex
    s = importdata(file, ';', r);
    x = s.data;
    data{i} = x;
    r = r + size(x,1) + h;
end

Then you can access your data using the curly bracket cell matrix notation然后您可以使用大括号单元矩阵表示法访问您的数据

ex = 2;
x = data{ex};

So you get所以你得到

x = [ 7  0
      8  1
      3  2 ]

For your second problem, you can add a loop to go through each file对于第二个问题,您可以通过每个文件向 go 添加一个循环

filenames = {'file1.csv', 'file2.csv', 'file3.csv'};
h = 2; % # header lines
num_ex = 3;
r = h; % track the current row in the file
data = cell(1,num_ex);
for i=1:num_ex
    for f=1:length(filenames)
        file = filenames{f};
        s = importdata(file, ';', r);
        x = s.data;
        data{i} = [data{i} x];
    end
    r = r + size(x,1) + h;
end

So that data{1} has all the data for ex 1, etc.因此该data{1}包含 ex 1 的所有数据等。

While I recognise this is not a full solution to your problem, an alternate solution I often use to overcome text editing (and the horrific speed penalty you get when parsing in MATLAB) is to load your data in through the MATLAB connectors to Java or C#.虽然我认识到这不是您问题的完整解决方案,但我经常用来克服文本编辑(以及在 MATLAB 中解析时遇到的可怕速度损失)的替代解决方案是通过 MATLAB 连接器将您的数据加载到 Java2337D74EFADB19FBE42D .

It's fairly easy to call C# and Java from MATLAB, and I do a lot of my text stuff that was.从 MATLAB 调用 C# 和 Java 相当容易,而且我做了很多我的文字工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM