From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to Import Several Text Files Into Matlab?
Date: Sun, 25 Nov 2012 16:09:22 -0600
Organization: NNTP Server
Lines: 35
Message-ID: <k8u508$mov$>
References: <k8trk4$g1f$> <k8ts30$tn7$>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20120306 Thunderbird/3.1.20
X-Notice: Filtered by postfilter v. 0.8.2
Xref: comp.soft-sys.matlab:783460

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);

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)];