How to concatenate multiple text files in a single matrix
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Benjamin Thompson
il 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 Commenti
Vedere anche
Categorie
Scopri di più su Cell Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!