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:
How can I process a sequence of files?

Subject: How can I process a sequence of files?

From: Ryan

Date: 24 Oct, 2012 02:59:08

Message: 1 of 4

How can I process a sequence of files?
Hello Matlab experts. I’m trying to figure out a way to read all CSV files in a directory, and similarly, I’m trying to figure out a way to read all text files in a directory.

I tried the following code. This compiles, and runs fine, but it doesn’t do anything close to what I want to do.
p=dir('C:\AllCSVFiles1\*.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:\AllCSVFiles1\', p(i).name]);
end

The following compiles and runs, but it doesn’t seem to be any better than the first code sample:
dirname = uigetdir;
Files = dir(fullfile(dirname,'*.csv'))
j =0;
for k = 1:numel(Files)
  filename = fullfile(dirname,Files(k).name);
  x = dlmread(filename,'\t'); %# I assume tab-delimiter
  j = j + size(x, 1);
  xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
end


Basically, I’m trying to import several different files (all files in a folder) into a single Matlab variable (data from 1st file is imported, data from 2nd file is stacked under the 1st, data from 3rd file is stacked under the 2nd, etc.). Or, as an alternative, data from each file is put into it’s own respective variable. Does anyone have any idea about how I can do this???

Thanks!!

Subject: How can I process a sequence of files?

From: TideMan

Date: 24 Oct, 2012 03:48:09

Message: 2 of 4

On Wednesday, October 24, 2012 3:59:08 PM UTC+13, Ryan wrote:
> How can I process a sequence of files?
>
> Hello Matlab experts. Im trying to figure out a way to read all CSV files in a directory, and similarly, Im trying to figure out a way to read all text files in a directory.
>
>
>
> I tried the following code. This compiles, and runs fine, but it doesnt do anything close to what I want to do.
>
> p=dir('C:\AllCSVFiles1\*.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:\AllCSVFiles1\', p(i).name]);
>
> end
>
>
>
> The following compiles and runs, but it doesnt seem to be any better than the first code sample:
>
> dirname = uigetdir;
>
> Files = dir(fullfile(dirname,'*.csv'))
>
> j =0;
>
> for k = 1:numel(Files)
>
> filename = fullfile(dirname,Files(k).name);
>
> x = dlmread(filename,'\t'); %# I assume tab-delimiter
>
> j = j + size(x, 1);
>
> xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
>
> end
>
>
>
>
>
> Basically, Im trying to import several different files (all files in a folder) into a single Matlab variable (data from 1st file is imported, data from 2nd file is stacked under the 1st, data from 3rd file is stacked under the 2nd, etc.). Or, as an alternative, data from each file is put into its own respective variable. Does anyone have any idea about how I can do this???
>
>
>
> Thanks!!

Your first method should work.
What is wrong with the result?

Subject: How can I process a sequence of files?

From: Ryan

Date: 24 Oct, 2012 04:23:09

Message: 3 of 4

I tried the code from here:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/156967

%let user select directory from UI,
%and pass this directory as a parameter
function ProcessFiles(directory)
allFiles = dir(directory);
for k = 1 : length(allFiles)
if(strfind(allFiles(k).name, '.csv'))
processFile([directory '\' allFiles(k).name]);
end
end
end

I put that in the Editor, set a Breakpoint, and hit F11 a few times; I get this error:
Error using ProcessFiles (line 4)
Not enough input arguments.


That doesn’t do anything useful for me. Any ideas???


"Ryan" wrote in message <k67lhs$qf4$1@newscl01ah.mathworks.com>...
> How can I process a sequence of files?
> Hello Matlab experts. I’m trying to figure out a way to read all CSV files in a directory, and similarly, I’m trying to figure out a way to read all text files in a directory.
>
> I tried the following code. This compiles, and runs fine, but it doesn’t do anything close to what I want to do.
> p=dir('C:\AllCSVFiles1\*.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:\AllCSVFiles1\', p(i).name]);
> end
>
> The following compiles and runs, but it doesn’t seem to be any better than the first code sample:
> dirname = uigetdir;
> Files = dir(fullfile(dirname,'*.csv'))
> j =0;
> for k = 1:numel(Files)
> filename = fullfile(dirname,Files(k).name);
> x = dlmread(filename,'\t'); %# I assume tab-delimiter
> j = j + size(x, 1);
> xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
> end
>
>
> Basically, I’m trying to import several different files (all files in a folder) into a single Matlab variable (data from 1st file is imported, data from 2nd file is stacked under the 1st, data from 3rd file is stacked under the 2nd, etc.). Or, as an alternative, data from each file is put into it’s own respective variable. Does anyone have any idea about how I can do this???
>
> Thanks!!

Subject: How can I process a sequence of files?

From: Ryan

Date: 24 Oct, 2012 04:29:08

Message: 4 of 4

Thanks for the look, TideMan. Here is the image that I see:
http://postimage.org/image/akp1ml5yn/

This seems to be a free image hosting site. I've never tried it before today. Can you see the pic???

TideMan <mulgor@gmail.com> wrote in message <37a94bd9-3381-4093-8c94-1aca939b6cc5@googlegroups.com>...
> On Wednesday, October 24, 2012 3:59:08 PM UTC+13, Ryan wrote:
> > How can I process a sequence of files?
> >
> > Hello Matlab experts. Im trying to figure out a way to read all CSV files in a directory, and similarly, Im trying to figure out a way to read all text files in a directory.
> >
> >
> >
> > I tried the following code. This compiles, and runs fine, but it doesnt do anything close to what I want to do.
> >
> > p=dir('C:\AllCSVFiles1\*.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:\AllCSVFiles1\', p(i).name]);
> >
> > end
> >
> >
> >
> > The following compiles and runs, but it doesnt seem to be any better than the first code sample:
> >
> > dirname = uigetdir;
> >
> > Files = dir(fullfile(dirname,'*.csv'))
> >
> > j =0;
> >
> > for k = 1:numel(Files)
> >
> > filename = fullfile(dirname,Files(k).name);
> >
> > x = dlmread(filename,'\t'); %# I assume tab-delimiter
> >
> > j = j + size(x, 1);
> >
> > xlswrite( 'sample_output', x, 'Sheet1',sprintf('A%d',j) )
> >
> > end
> >
> >
> >
> >
> >
> > Basically, Im trying to import several different files (all files in a folder) into a single Matlab variable (data from 1st file is imported, data from 2nd file is stacked under the 1st, data from 3rd file is stacked under the 2nd, etc.). Or, as an alternative, data from each file is put into its own respective variable. Does anyone have any idea about how I can do this???
> >
> >
> >
> > Thanks!!
>
> Your first method should work.
> What is wrong with the result?

Tags for this Thread

No tags are associated with 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