Import multiple text file with one header

1 view (last 30 days)
mkmat
mkmat on 11 Aug 2014
Commented: Michael Haderlein on 13 Aug 2014
Hello everyone,
There are several questions and answers on the site; however, I have a bit of issue by combining several text files.
I have two text files, the first one with headers and the second one without.
I would like to merge them into one file, called 'data' preferably using cell2mat format, and also have a separate variable that shows the headers.
---------
file1.txt
a b c
1 2 3
4 5 6
file2.txt
7 8 9
10 11 12
---------
any help much appreciated!
Many thanks,

Answers (3)

Michael Haderlein
Michael Haderlein on 11 Aug 2014
files=dir('test*.txt');
output='data.txt';
fidout=fopen(output,'w');
for cnt=1:length(files)
fprintf(fidout,'%s\n',files(cnt).name);
fidin=fopen(files(cnt).name);
fwrite(fidout,fread(fidin));
fprintf(fidout,'\n');
fclose(fidin);
end
fclose(fidout);
will merge the files and add the file names. What do you mean with a separate variable that shows the headers? If you want to store the headers in a variable, you can preallocate the headers before the loop:
headers=cell(size(files));
then save the first line of each file if there are letters:
fidin=fopen(files(cnt).name); %until here as in the upper code
first_line=fgets(fidin);
if any(isstrprop(first_line,'alpha'))
headers{cnt}=first_line;
end
fwrite(fidout,first_line);
fwrite(fidout,fread(fidin)); %continue with the upper code
  1 Comment
Michael Haderlein
Michael Haderlein on 13 Aug 2014
Ok, maybe this time the code does what you want...
files=dir('test*.txt');
output='data.txt';
fidout=fopen(output,'w');
headers=cell(size(files));
numbers=cell(size(files));
for cnt=1:length(files)
fprintf(fidout,'%s\n',files(cnt).name);
fidin=fopen(files(cnt).name);
temp=fscanf(fidin,'%c');
if isstrprop(temp(1),'alpha')
headers{cnt}=temp(1:strfind(temp,sprintf('\n'))-2);
temp(1:strfind(temp,sprintf('\n')))='';
end
numbers{cnt}=str2num(temp);
fprintf(fidout,temp);
fprintf(fidout,'\n');
fclose(fidin);
end
fclose(fidout);
In case all files have the same number of columns, you can convert the numbers cell to an array:
numbers=cell2mat(numbers);
Please note that in this example I assume that the line break in the text files is \cr\lf (ascii codes 13 10). If your file has a different line break, you might need to change the -2 to -1 and change the line separator.

Sign in to comment.


mkmat
mkmat on 11 Aug 2014
I tried what you proposed.
name of the file got into the 'data.txt' file as well. The first line of the data.txt file is:
file1.txt and so forth.
As you said, I would like to save the headers in a separate variable, eliminate them from the files and save rest of the file, which are numbers, in a matlab array or preferably matrix.
  1 Comment
Michael Haderlein
Michael Haderlein on 12 Aug 2014
Ok, your question is different from the first one. Do I understand correctly, in the data.txt you do not want to have the headers but only the numbers? And you want to get two variables, one with the headers and one with the data? "eliminate them from the files": Do you want to remove the headers from the input files?

Sign in to comment.


mkmat
mkmat on 12 Aug 2014
Yes, there shouldn't be any headers in the data.txt file, only numbers or other characters.
The headers should be stored in a separate variable in MATLAB

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!