How to concatenate multiple text files in a single matrix
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Benjamin Thompson
2022 年 4 月 28 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!