Reputation: 121
I have lots of .csv files that I want to read into MATLAB, but doing some organization along the way
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. 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. 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. I need to match up ex's within files based on matching one of the lines of header (e.g. whenever it is called 'track1').
This is how the actual data looks like.
Upvotes: 0
Views: 2720
Reputation: 1805
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#.
It's fairly easy to call C# and Java from MATLAB, and I do a lot of my text stuff that was.
Upvotes: 1
Reputation: 16440
Since the number of rows in each ex is different, you must use a cell matrix.
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
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.
Upvotes: 1