user1205030
user1205030

Reputation: 225

Import multiple CSV in a file

I have tried to import several csv files into one file. However, the new file overwrites the "original" ones. Only the last processed one among them was imported. Something may be wrong about the loop, but I don't know where to change.

This is what I have:

p=dir('C:\foldername\*.csv');
for i=1:length(p)
     [num, text, all]= xlsread(['C:\foldername\', p(i).name]);
end

Upvotes: 0

Views: 2165

Answers (2)

Ruofeng
Ruofeng

Reputation: 2340

You cannot read all the things into the same variables, but you can put them in different dimensions.

p=dir('C:\foldername\*.csv');
num = cell(size(p));
text = cell(size(p));
all = cell(size(p));
for i=1:length(p)
    [num{i}, text{i}, all{i}]= xlsread(['C:\foldername\', p(i).name]);
end

Upvotes: 0

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

You are overriding the variables in the loop. Try to collect everything in cell array:

num = {};
text = {};
all = {};
p=dir('C:\foldername\*.csv');
for i=1:length(p)
    [num{end+1}, text{end+1}, all{end+1}]= xlsread(['C:\foldername\', p(i).name]);
end

Upvotes: 1

Related Questions