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 to Import Several Text Files Into Matlab?

Subject: How to Import Several Text Files Into Matlab?

From: Ryan

Date: 25 Nov, 2012 19:30:12

Message: 1 of 5

Ok, I give up. I’ve searched online for hours for a script that will import several Text files in a folder into a single variable in Matlab. I have a script that imports several Text files into several variables in Matlab, but I can’t figure out how to import several Text files into a single variable. Does anyone know how to do this?

Subject: How to Import Several Text Files Into Matlab?

From: dpb

Date: 25 Nov, 2012 19:37:15

Message: 2 of 5

On 11/25/2012 1:30 PM, Ryan wrote:
> Ok, I give up. I’ve searched online for hours for a script that will
> import several Text files in a folder into a single variable in Matlab.
> I have a script that imports several Text files into several variables
> in Matlab, but I can’t figure out how to import several Text files into
> a single variable. Does anyone know how to do this?

concatenate the second onto the first, the third onto that, etc., etc., ...


--

Subject: How to Import Several Text Files Into Matlab?

From: dpb

Date: 25 Nov, 2012 22:09:22

Message: 3 of 5

On 11/25/2012 1:37 PM, dpb wrote:
> On 11/25/2012 1:30 PM, Ryan wrote:
>> Ok, I give up. I’ve searched online for hours for a script that will
>> import several Text files in a folder into a single variable in Matlab.
>> I have a script that imports several Text files into several variables
>> in Matlab, but I can’t figure out how to import several Text files into
>> a single variable. Does anyone know how to do this?
>
> concatenate the second onto the first, the third onto that, etc., etc., ...

or, use a cell array.

To read all files that match *.txt--

d = dir('*.txt');
nfiles = length(d);
data = cell(1, nfiles);
for k = 1:nfiles
   data{k} = importdata(d(k).name);
end

Above is cell array...can handle case where not all files have same
shape of input. If all have same number of columns and want a single
array, then just replace the cell reference w/ concatenation but be
aware that the dynamic reallocation may become slow w/ a large number of
and/or very large files...

d = dir('*.txt');
nfiles = length(d);
data = [];
for k = 1:nfiles
   data = [data; importdata(d(k).name)];
end

--

Subject: How to Import Several Text Files Into Matlab?

From: Ryan

Date: 26 Nov, 2012 01:51:10

Message: 4 of 5

Wow! Thanks so much! The second script worked perfect!! I'm not sure about the first one though. It ran, but it didn't seem to do anything useful. All it did was give me this:
<9x1 cell> <9x1 cell> <9x1 cell>

Thanks again!!


dpb <none@non.net> wrote in message <k8u508$mov$1@speranza.aioe.org>...
> On 11/25/2012 1:37 PM, dpb wrote:
> > On 11/25/2012 1:30 PM, Ryan wrote:
> >> Ok, I give up. I’ve searched online for hours for a script that will
> >> import several Text files in a folder into a single variable in Matlab.
> >> I have a script that imports several Text files into several variables
> >> in Matlab, but I can’t figure out how to import several Text files into
> >> a single variable. Does anyone know how to do this?
> >
> > concatenate the second onto the first, the third onto that, etc., etc., ...
>
> or, use a cell array.
>
> To read all files that match *.txt--
>
> d = dir('*.txt');
> nfiles = length(d);
> data = cell(1, nfiles);
> for k = 1:nfiles
> data{k} = importdata(d(k).name);
> end
>
> Above is cell array...can handle case where not all files have same
> shape of input. If all have same number of columns and want a single
> array, then just replace the cell reference w/ concatenation but be
> aware that the dynamic reallocation may become slow w/ a large number of
> and/or very large files...
>
> d = dir('*.txt');
> nfiles = length(d);
> data = [];
> for k = 1:nfiles
> data = [data; importdata(d(k).name)];
> end
>
> --

Subject: How to Import Several Text Files Into Matlab?

From: dpb

Date: 26 Nov, 2012 02:21:32

Message: 5 of 5

On 11/25/2012 7:51 PM, Ryan wrote:

...[top posting repaired--don't do that; hard follow conversation makes]...

> dpb <none@non.net> wrote in message <k8u508$mov$1@speranza.aioe.org>...
>> On 11/25/2012 1:37 PM, dpb wrote:
>> > On 11/25/2012 1:30 PM, Ryan wrote:
>> >> Ok, I give up. I’ve searched online for hours for a script that will
>> >> import several Text files in a folder into a single variable in
...

>> To read all files that match *.txt--
>>
>> d = dir('*.txt');
>> nfiles = length(d);
>> data = cell(1, nfiles);
>> for k = 1:nfiles
>> data{k} = importdata(d(k).name);
>> end
>>
>> Above is cell array...can handle case where not all files have same
>> shape of input. If all have same number of columns and want a single
>> array, then just replace the cell reference w/ concatenation but be
>> aware that the dynamic reallocation may become slow w/ a large number
>> of and/or very large files...
>>
>> d = dir('*.txt');
>> nfiles = length(d);
>> data = [];
>> for k = 1:nfiles
>> data = [data; importdata(d(k).name)];
>> end
>>
 > Wow! Thanks so much! The second script worked perfect!! I'm not sure
 > about the first one though. It ran, but it didn't seem to do anything
 > useful. All it did was give me this:
 > <9x1 cell> <9x1 cell> <9x1 cell>

Well, dereference the cell arrays w/ the curly brackets...

Read the "Getting Started" sections on data types and specifically cell
arrays...

--

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