Reading multiple txt files and placing numerical data in a matrix
Show older comments
Hi all, I'm trying to read multiple file text without the header and pile up their numerical content. I have attached an example txt file.
Here is the basic code I use:
[filename,pathname,d]=uigetfile('*.txt','MultiSelect','on');
filepath=fullpath(pathname,filename);
a=readtable(filepath);
a=table2array(a);
It works for 1 fIle but gives an error for more files selected. And then once I imported correctly multiple files, how do I pile their numerical content? I guess have to insert that in a for cycle. Bear in mind that different files can have different number of columns and rows.
Thanks in advance to anyone who will help me
4 Comments
Chris
on 24 Apr 2022
If files can have different numbers of columns and rows, what sort of "pile" are you expecting? How would the data be related?
Mathieu NOE
on 25 Apr 2022
hello
maybe it would be helful to share some text files so we can help you better
tx
Jan
on 25 Apr 2022
"but gives an error for more files selected" - whenever you mention an error in the forum, attach a copy of the complete error message instead of letting the readers guess, what you can see already.
Jeremy Hughes
on 25 Apr 2022
a = readtable(filepath);
a = table2array(a);
Should be equivalent to:
a = readmatrix(filepath)
Answers (1)
Jan
on 25 Apr 2022
[filename, pathname] = uigetfile('*.txt','MultiSelect','on');
filename = cellstr(filename);
data = [];
for k = 1:numel(filename)
filepath = fullpath(pathname,filename);
tab = readtable(filepath);
data = cat(1, data, table2array(tab));
end
5 Comments
Iacopo
on 25 Apr 2022
Mathieu NOE
on 26 Apr 2022
Which matlab release are you running ?
Iacopo
on 26 Apr 2022
Jan
on 26 Apr 2022
@lacopo: I've copied "fullpath" from your example in the question. Maybe "fullfile" is meant.
Iacopo
on 26 Apr 2022
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!