Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Import multiple data files

Subject: Import multiple data files

From: Wen Zhe

Date: 27 Jul, 2010 11:06:04

Message: 1 of 11

Hi all,

Just a simple question concering important multiple data onto Matlab.

I've rewritten the following code so far and all seems to be working well:

files = dir('*.csv');

for i=1:length(files)
  
eval(['importdata ' files(i).name ' ']);

end

Here's the thing. I want to save each data file as a seperate unique variable. How do i do that? Currently, it's overwriting a variable called 'ans'.

Hope to get some assistance soon. Thanks!

Wen

Subject: Import multiple data files

From: us

Date: 27 Jul, 2010 11:20:05

Message: 2 of 11

"Wen Zhe " <wenzhe2092@hotmail.com> wrote in message <i2meis$lbh$1@fred.mathworks.com>...
> Hi all,
>
> Just a simple question concering important multiple data onto Matlab.
>
> I've rewritten the following code so far and all seems to be working well:
>
> files = dir('*.csv');
>
> for i=1:length(files)
>
> eval(['importdata ' files(i).name ' ']);
>
> end
>
> Here's the thing. I want to save each data file as a seperate unique variable. How do i do that? Currently, it's overwriting a variable called 'ans'.
>
> Hope to get some assistance soon. Thanks!
>
> Wen

a hint:

http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F

us

Subject: Import multiple data files

From: Srikanth

Date: 27 Jul, 2010 13:18:21

Message: 3 of 11

Please try this:

for j=1:i
    a(j,1) = importdata(files(j).name);
end

"Wen Zhe " <wenzhe2092@hotmail.com> wrote in message <i2meis$lbh$1@fred.mathworks.com>...
> Hi all,
>
> Just a simple question concering important multiple data onto Matlab.
>
> I've rewritten the following code so far and all seems to be working well:
>
> files = dir('*.csv');
>
> for i=1:length(files)
>
> eval(['importdata ' files(i).name ' ']);
>
> end
>
> Here's the thing. I want to save each data file as a seperate unique variable. How do i do that? Currently, it's overwriting a variable called 'ans'.
>
> Hope to get some assistance soon. Thanks!
>
> Wen

Subject: Import multiple data files

From: Wen Zhe

Date: 27 Jul, 2010 13:30:15

Message: 4 of 11

Hi,

I gave this a go but the following error message appeared:

??? Subscripted assignment between dissimilar structures.

Why is this the case? Note my csv files contain headers and the numerical data which i am more interested in.

Many thanks.

"Srikanth " <neevenenu@yahoo.com> wrote in message <i2mmat$fmq$1@fred.mathworks.com>...
> Please try this:
>
> for j=1:i
> a(j,1) = importdata(files(j).name);
> end
>
> "Wen Zhe " <wenzhe2092@hotmail.com> wrote in message <i2meis$lbh$1@fred.mathworks.com>...
> > Hi all,
> >
> > Just a simple question concering important multiple data onto Matlab.
> >
> > I've rewritten the following code so far and all seems to be working well:
> >
> > files = dir('*.csv');
> >
> > for i=1:length(files)
> >
> > eval(['importdata ' files(i).name ' ']);
> >
> > end
> >
> > Here's the thing. I want to save each data file as a seperate unique variable. How do i do that? Currently, it's overwriting a variable called 'ans'.
> >
> > Hope to get some assistance soon. Thanks!
> >
> > Wen

Subject: Import multiple data files

From: Wen Zhe

Date: 27 Jul, 2010 13:33:06

Message: 5 of 11

Hi us,

Thanks for the help link. Ermm, i gave it a show and here's the new code i'm trying:

csvFiles = dir('*.csv');
for k = 1:length(csvFiles)
  filename = csvFiles(k).name;
  data1 = importdata(filename);
end

It still doesn't seem to be running though. Is the 'importdata' line correct? Note that my csv files contain both text and the numerical data which i'm more interested in.

Many thanks.

"us " <us@neurol.unizh.ch> wrote in message <i2mfd5$edl$1@fred.mathworks.com>...
> "Wen Zhe " <wenzhe2092@hotmail.com> wrote in message <i2meis$lbh$1@fred.mathworks.com>...
> > Hi all,
> >
> > Just a simple question concering important multiple data onto Matlab.
> >
> > I've rewritten the following code so far and all seems to be working well:
> >
> > files = dir('*.csv');
> >
> > for i=1:length(files)
> >
> > eval(['importdata ' files(i).name ' ']);
> >
> > end
> >
> > Here's the thing. I want to save each data file as a seperate unique variable. How do i do that? Currently, it's overwriting a variable called 'ans'.
> >
> > Hope to get some assistance soon. Thanks!
> >
> > Wen
>
> a hint:
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F
>
> us

Subject: Import multiple data files

From: us

Date: 27 Jul, 2010 13:37:54

Message: 6 of 11

On Jul 27, 3:33 pm, "Wen Zhe " <wenzhe2...@hotmail.com> wrote:
> Hi us,
>
> Thanks for the help link. Ermm, i gave it a show and here's the new code i'm trying:
>
> csvFiles = dir('*.csv');
> for k = 1:length(csvFiles)
>   filename = csvFiles(k).name;
>   data1 = importdata(filename);
> end
>
> It still doesn't seem to be running though. Is the 'importdata' line correct? Note that my csv files contain both text and the numerical data which i'm more interested in.

be more precise: which part is NOT working(?); WHAT do you expect from
the output/loop(?)...

us

Subject: Import multiple data files

From: Wen Zhe

Date: 27 Jul, 2010 13:47:41

Message: 7 of 11

Apologies for being unclear. I've re-ran the code and it worked fine. Here's the problem: i have three csv files and i want to import them onto Matlab as three seperate VARIABLES. With the current code, the output would be 'data1', but that would contain the data from the last csv file.

So, is there any way to store my csv files in seperate variables without overwriting any of them? Any help appreciated. Thanks.

us <us@neurol.unizh.ch> wrote in message <8319afb9-1b51-4c85-a02d-5e609922c02d@t35g2000vbb.googlegroups.com>...
> On Jul 27, 3:33 pm, "Wen Zhe " <wenzhe2...@hotmail.com> wrote:
> > Hi us,
> >
> > Thanks for the help link. Ermm, i gave it a show and here's the new code i'm trying:
> >
> > csvFiles = dir('*.csv');
> > for k = 1:length(csvFiles)
> >   filename = csvFiles(k).name;
> >   data1 = importdata(filename);
> > end
> >
> > It still doesn't seem to be running though. Is the 'importdata' line correct? Note that my csv files contain both text and the numerical data which i'm more interested in.
>
> be more precise: which part is NOT working(?); WHAT do you expect from
> the output/loop(?)...
>
> us

Subject: Import multiple data files

From: Andy

Date: 27 Jul, 2010 14:01:08

Message: 8 of 11

"Wen Zhe " <wenzhe2092@hotmail.com> wrote in message <i2mo1t$a1s$1@fred.mathworks.com>...
> Apologies for being unclear. I've re-ran the code and it worked fine. Here's the problem: i have three csv files and i want to import them onto Matlab as three seperate VARIABLES. With the current code, the output would be 'data1', but that would contain the data from the last csv file.
>
> So, is there any way to store my csv files in seperate variables without overwriting any of them? Any help appreciated. Thanks.

Perhaps you should consider storing the data in a cell array:

csvFiles = dir('*.csv');

filename = cell(length(csvFiles),1); % preallocate
data = cell(length(csvFiles),1); % preallocate

for k = 1:length(csvFiles)
  filename{k} = csvFiles(k).name;
  data{k} = importdata(filename);
end

Subject: Import multiple data files

From: us

Date: 27 Jul, 2010 14:04:32

Message: 9 of 11

On Jul 27, 4:01 pm, "Andy " <myfakeemailaddr...@gmail.com> wrote:
> "Wen Zhe " <wenzhe2...@hotmail.com> wrote in message <i2mo1t$a1...@fred.mathworks.com>...
>
> > Apologies for being unclear. I've re-ran the code and it worked fine. Here's the problem: i have three csv files and i want to import them onto Matlab as three seperate VARIABLES. With the current code, the output would be 'data1', but that would contain the data from the last csv file.
>
> > So, is there any way to store my csv files in seperate variables without overwriting any of them? Any help appreciated. Thanks.
>
> Perhaps you should consider storing the data in a cell array:
>
> csvFiles = dir('*.csv');
>
> filename = cell(length(csvFiles),1); % preallocate
> data = cell(length(csvFiles),1); % preallocate
>
> for k = 1:length(csvFiles)
>   filename{k} = csvFiles(k).name;
>   data{k} = importdata(filename);
> end

well...
either

     filename=csvFiles(k).name;

or

     data{k}=importdata(filename{k});

:-)
us

Subject: Import multiple data files

From: Walter Roberson

Date: 27 Jul, 2010 14:08:10

Message: 10 of 11

Wen Zhe wrote:

a = cell(i,1);
for j=1:i
   a{j} = importdata(files(j).name);
end

Subject: Import multiple data files

From: Wen Zhe

Date: 27 Jul, 2010 14:25:05

Message: 11 of 11

Hey guys!

Great idea to use cell arrays. I actually did use 'genvarname' to assign my csv files to several variables. But your idea of cell arrays stores it as one variable.

So if i were to plot a graph with two columns in my csv files (which are in the cell array 'data'), how would i be able to call it?

Thanks again.

us <us@neurol.unizh.ch> wrote in message <40d1eff0-56cc-4db2-b9c9-ba9ffbbc7afd@b5g2000vbl.googlegroups.com>...
> On Jul 27, 4:01 pm, "Andy " <myfakeemailaddr...@gmail.com> wrote:
> > "Wen Zhe " <wenzhe2...@hotmail.com> wrote in message <i2mo1t$a1...@fred.mathworks.com>...
> >
> > > Apologies for being unclear. I've re-ran the code and it worked fine. Here's the problem: i have three csv files and i want to import them onto Matlab as three seperate VARIABLES. With the current code, the output would be 'data1', but that would contain the data from the last csv file.
> >
> > > So, is there any way to store my csv files in seperate variables without overwriting any of them? Any help appreciated. Thanks.
> >
> > Perhaps you should consider storing the data in a cell array:
> >
> > csvFiles = dir('*.csv');
> >
> > filename = cell(length(csvFiles),1); % preallocate
> > data = cell(length(csvFiles),1); % preallocate
> >
> > for k = 1:length(csvFiles)
> >   filename{k} = csvFiles(k).name;
> >   data{k} = importdata(filename);
> > end
>
> well...
> either
>
> filename=csvFiles(k).name;
>
> or
>
> data{k}=importdata(filename{k});
>
> :-)
> us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us