Importing multiple text files into MATLAB

I was trying to import and read a series of text files. i was able to read the data for one file at a time. Howevwer while using the array indexing via the for loop, all the files could not be read at once
It gives an error saying
% subscripted assignment dimension mismatch.
p=dir('*.txt');
N=length(p);
for k=1:N
[fid]=fopen(p(k).name,'r');
A(k)=cell2mat(textscan(fid, '%f %f', 'HeaderLines', 4));
fid=fclose(fid);
end

 Accepted Answer

The options you have available to you depend on the contents of your txt files, but one general option (i.e., will work regardless) is to use a cell array:
p=dir('*.txt');
N=length(p);
A = cell(1,N); % cell array A
for k=1:N
% [fid]=fopen(p(k).name,'r');
fid = fopen(p(k).name,'r');
% A(k)=cell2mat(textscan(fid, '%f %f', 'HeaderLines', 4));
A{k} = cell2mat(textscan(fid, '%f %f', 'HeaderLines', 4)); % use {} for indexing A
% fid=fclose(fid);
fclose(fid);
end
Now each element of A is a cell containing the contents of one txt file. Access those contents again using curly braces {}:
A{1} % contents of the first file
A{2} % contents of the second file
A{end} % contents of the last file
% etc.

4 Comments

Thank you. That fixed the problem that i've been having. These were the files i was trying to import and read the data from .
Out of curiosity what would be the option to acccess the contents of these files?
To be clear, I meant the options for storing the data in variables in your code depend on the contents. But the options for reading the files do too.
As for methods of reading the files, I think using textscan like you are doing is fine. You could also use readmatrix.
p=dir('*.txt');
N=length(p);
% using textscan
A = cell(1,N);
for k=1:N
fid = fopen(p(k).name,'r');
A{k} = cell2mat(textscan(fid, '%f %f', 'HeaderLines', 4));
fclose(fid);
end
% using readmatrix
B = cell(1,N);
for k=1:N
B{k} = readmatrix(p(k).name,'NumHeaderLines',4);
B{k}(end,:) = []; % remove the last row, corresponding to the last line in the file, which just contains a parenthesis
end
isequal(A,B) % same result either way
ans = logical
1
As for ways to store the data in a variable, if the matrix in each file was the same size, then you could use a 3D array to store them all. But in this case some are 65x2 and some are 66x2:
A
A = 1×5 cell array
{65×2 double} {66×2 double} {66×2 double} {65×2 double} {66×2 double}
so they have to be stored in some container class, like a cell array.
Or you could use a structure array, which you already have, in fact. Merely store the data from each file in a new field called 'data' (or whatever) in each element of the array of structures p:
p=dir('*.txt');
N=length(p);
% using textscan
A = cell(1,N);
for k=1:N
fid = fopen(p(k).name,'r');
p(k).data = cell2mat(textscan(fid, '%f %f', 'HeaderLines', 4));
fclose(fid);
end
% now the data is in p
p
p = 5×1 struct array with fields:
name folder date bytes isdir datenum data
p(1)
ans = struct with fields:
name: 'yu_0.8.txt' folder: '/users/mss.system.SHXc4o' date: '05-Jul-2022 18:36:18' bytes: 1328 isdir: 0 datenum: 7.3871e+05 data: [65×2 double]
p(2)
ans = struct with fields:
name: 'yu_1.2.txt' folder: '/users/mss.system.SHXc4o' date: '05-Jul-2022 18:36:18' bytes: 1340 isdir: 0 datenum: 7.3871e+05 data: [66×2 double]
Thank you! I understood it.
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2016b

Asked:

on 30 Jun 2022

Commented:

on 22 Jul 2022

Community Treasure Hunt

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

Start Hunting!