Import and combine multiple XYZ.txt data files into one X, Y and Z column vectors

Hi
I have a large number of txt files with different names, each one has tab seperated values of X Y and Z data in 3 columns. I have been importing each file seperately but there must be a way to import all the files at once into just 3 column vectors? The files themselves are very large. Is it also possible to write the data into one txt file?
Any help on this would be much appreciated.
Thanks

 Accepted Answer

I don't think you can import all the files at once, but you can do so in a loop and append the data from each file to what has already been imported by concatenating the arrays. See the MATLAB FAQ for more details.
HTH,
Arnaud

4 Comments

Hi Arnaud
I am still having problems, am fairly new with Matlab and any more help would be appreciated. I am trying something like this to import all of the data:
files=dir('*.txt');
for i=1:length(files);
fid=fopen(files);
C=textscan(fid,'%f%f%f','Delimiter','tab','HeaderLines',1);
fclose(fid)
to import the data but its not working as I get error using textscan first input can not be empty? And somehow as well as importing the data (which is several large txt files each with Easting, Northing and Depth information) I need to combine the data from each into three single column vectors X,Y and Z. I know how to do this one at a time by concatenating single arrays but this is tedious, is there some code I could write to do it all?
Many thanks
The error message seems to suggest that fid is empty. What you need to do is:
for i=1:length(files)
fid = fopen(files(i));
C{i} = textscan(fid, etc...);
fclose(fid)
end
You then need to write some code to extract the X Y and Z data from each cell array C{i}. You can probably do this in the loop, but without knowing what C{i} is like, it's difficult to tell exactly.
thanks for helping but I still cant get it to work.. this is what I get now...
files=dir('*.txt')
for i=1:length(files)
fid = fopen(files(i));
C{i} = textscan(fid,'%f%f%f','Delimiter','tab','HeaderLines',1);
fclose(fid)
end
files =
15x1 struct array with fields:
name
date
bytes
isdir
datenum
??? Error using ==> textscan
First input can not be empty.
Check out what's 'files' in your workspace, it's probably a structure (for more details see the documentation for dir). You need to use files(i).name with fopen.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!