How to read and write multiple .txt files with unpatterned names into a table?
13 views (last 30 days)
I have a multiple .txt files (31) each containing 4 columns with headers and 2000 rows.
What I am trying to do is, to import the first 1000 column for each file and combine them into a table by horizontal concatenation and do the same for the second 1000 column to obtain a second table than write them into 2 separate excel files.
I have read most of the related headers but it seems a bit challenging to manage the files with unpatterned names. I have attached a couple of the .txt files for an example.
I used the "generate function" from the import tool which works fine, but does the job for only a single .txt file. Is it possible to insert a loop into this auto generated function? Or is there a simpler way?
function mA = importfile1(filename, dataLines)
%IMPORTFILE1 Import data from a text file
% MA = IMPORTFILE1(FILENAME) reads data from text file FILENAME for the
% default selection. Returns the data as a table.
% MA = IMPORTFILE1(FILE, DATALINES) reads data for the specified row
% interval(s) of text file FILENAME. Specify DATALINES as a positive
% scalar integer or a N-by-2 array of positive scalar integers for
% dis-contiguous row intervals.
% mA = importfile1("C:\Users\uzumcu\Desktop\MatlabDrive\QCL measurements\22-06-20\1726 RG 2.2 SG 2 Current\400mA.txt", [2, 1002]);
% See also READTABLE.
% Auto-generated by MATLAB on 30-Jun-2020 17:33:31
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [2, Inf];
%% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 4);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["PointsReference", "AmplitudeReference", "PointsSample", "AmplitudeSample"];
opts.VariableTypes = ["double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
mA = readtable(filename, opts);
Any help could be really appreciated since I am trying to figure this out for 3 days.
Thank you all in advance
Steven Lord on 30 Jun 2020
Generate a list of the files you want to import. There are a number of different tools you can use to do this; since you say the file names are "unpatterned" the dir function will probably be the most useful as it will give a list of all files in the directory.
Create a combined table with the right number of variables and give those variables the correct names. Write a loop that iterates over the list of files. In the body of the loop call this generated function for the file to get the table for that file then append it to the combined table using concatenation ([A; B]), join, etc.