How to concatenate multiple text files in a single matrix

3 vues (au cours des 30 derniers jours)
Iacopo
Iacopo le 28 Avr 2022
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

Réponses (1)

Benjamin Thompson
Benjamin Thompson le 28 Avr 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.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by