Path: news.mathworks.com!newsfeed-00.mathworks.com!solaris.cc.vt.edu!news.vt.edu!news.glorb.com!aioe.org!.POSTED!not-for-mail
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: Aioe.org NNTP Server
Lines: 35
Message-ID: <k8u508$mov$1@speranza.aioe.org>
References: <k8trk4$g1f$1@newscl01ah.mathworks.com> <k8ts30$tn7$1@speranza.aioe.org>
NNTP-Posting-Host: 6i7PHBKk+sW0vS5oVtvugg.user.speranza.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20
X-Notice: Filtered by postfilter v. 0.8.2
Xref: news.mathworks.com 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);
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

--