How to concatenate multiple text files in a single matrix
조회 수: 4 (최근 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에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!