How to concatenate multiple text files in a single matrix
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
I'm trying to concatenate multiple text files in a single matrix and process it together (example files are attached).
I'm using the following code:
[filename, pathname] = uigetfile('*.txt','MultiSelect','on');
filepath = fullfile(pathname,filename);
filepath = string(filepath);
numfiles = length(filepath);
data = [];
tab=[];
for k = 1:numfiles
tab = readtable(filepath(k));
if k == 1
data = table2array(tab);
else
data = cat(numfiles, data, table2array(tab));
end
end
It works with 1 and 2 files but when I select 3 files together it gives me the following error message:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in readtxt5 (line 300)
data = cat(numfiles, data, table2array(tab));
Can anyone help me understand how to modify it to include 2+ files? Thanks
0 Kommentare
Antworten (1)
Benjamin Thompson
am 28 Apr. 2022
The third file does not have as many columns as the first two. Table concatenation requires a consistent number of variables to expand the table.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!