Merging 4th column multiple text files into one file

Hello, I want to do read column 4 of multiple text files (206) (all located in the same folder) and merge them into one file. My filenames consists of numbers for the first part, but the second part has a random name: 1802020100001_Grand-Maison-Dam.txt, 1802020100002_Vianden.txt, etc. The text files looks like this (all have the same number of rows (12) and columns (4)):
2015-01-31 6.05083 45.1453 2.17524
2015-02-28 6.05083 45.1453 0.267542
2015-03-31 6.05083 45.1453 8.07546
2015-04-30 6.05083 45.1453 30.029
2015-05-31 6.05083 45.1453 93.8143
2015-06-30 6.05083 45.1453 73.0604
2015-07-31 6.05083 45.1453 31.8136
2015-08-31 6.05083 45.1453 26.1006
2015-09-30 6.05083 45.1453 30.3608
2015-10-31 6.05083 45.1453 33.3809
2015-11-30 6.05083 45.1453 22.9509
2015-12-31 6.05083 45.1453 11.0744
I want to only read the fourth column of each textfile, transpose it to a row and add the rows below each other. In addition I want the first column to be the name of the file. So I want the output file to look like this:
Filename1 2.17524 0.267542 8.07546 30.029 93.8143 73.0604 31.8136 26.1006 30.3608 33.3809 22.9509 11.0744
Filename2 .. .. .. ..
I am new to matlab, I read some stuff about reading multiple text files and merging multiple files into one but I do not succeed to get the desired output and only have limited time to figure this out. Could someone help me with a code?

4 个评论

@Diede: "I read some stuff about reading multiple text files" - this makes it harder to post an answer. Now we know, that you have read something and repeating it would be a waste of time. But we do not know, what you have read already. Better post the code you have tried so far, such that we can improve it directly.
  • Read all the txt-files in the folder ?
  • Does the order of the rows in the result file matter ?
  • tab as delimiter ?
@per isakson:
  • Yes read all the txt-files in the folder
  • Yes the order matters, it should start with File1, and end with File with File 241. I have 206 files (not all numbers within 1 and 241 are in the folder).
  • Space as delimiter
@Jan simon: This is wat I have now
myFolder='C:\Matlab\flow_tables\climatology_percentile20\total_flow';
filePattern=fullfile(myFolder, '*.txt');
theFiles=dir(filePattern);
A=zeros(206,1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName=fullfile(myFolder, baseFileName);
A(fullFileName,1)=fprintf(1, 'Now reading %s\n',fullFileName);
for k = 1:size(fullFileName)
fileID=fopen(fullFileName);
C=textscan(fileID, '%s %f32 %f32 %f32');
fclose(fileID);
whos C
D=C{4}; %read only inflow columns
D=D'; %convert column to row
end
end
It is a combination of several codes I found on the internet. I tried something with A=zeros, as my results were not stored and it seems that it overwrites 'D' everytime, but does not work.

请先登录,再进行评论。

回答(1 个)

Folder = 'C:\Your\Folder';
DirList = dir(fullfile(Folder, '*.txt'));
OutFID = fopen(fullfile(Folder, 'Output.txt', 'w');
if OutFID == -1, error('Cannot open file for writing.'); end
for iFile = 1:numel(DirList)
InFile = fullfile(Folder, DirList(iFile).name);
InFID = fopen(InFile, 'r');
if InFID == -1, error('Cannot open file for writing.'); end
DataC = textscan(InFID, '%*s%*s%*s%s');
fclose(InFID);
fprintf(OutFID, '%s', DirList(iFile).name);
fprintf(OutFID, ' %s', DataC{1}{:});
fprintf(OutFID, '\n');
end
fclose(OutFID);
You can solve such a problem by dividing it into parts:
  1. Process a sequence of files (See: https://www.mathworks.com/matlabcentral/answers/57446-faq-how-can-i-process-a-sequence-of-files )
  2. Import the wanted data from one file
  3. Export the wanted data to the output
Then solve one step after the other.

1 个评论

I tried to use your code. But it cannot open the files. It says: Error using tryout (line 4) Cannot open file for writing.
The folder is right, I am sure of that. Could it have to do with the matlab version? Or what causes this?

请先登录,再进行评论。

提问:

2018-2-13

评论:

2018-2-13

Community Treasure Hunt

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

Start Hunting!

Translated by