Read multiple txt files and save them according to their name
Show older comments
Hello,
I would like to read multiple text files with names like A16.txt, B64.txt and save them like matrices in Matlab. Each of the files look like this below:
Time [s] Deformation Probe (X) [mm] Deformation Probe (Y) [mm] Deformation Probe (Z) [mm] Deformation Probe (Total) [mm]
1 1. -2.05900494e-002 9.53490478e-002 -5.00916689e-002 9.67559114e-002
2 2. -9.77241325e-003 0.06292072e-002 0.94899105e-003 5.13229044e-002
3 3. 5.98193922e-003 -8.41211991e-003 6.169048816 4.169208785
4 4. 2.79957587e-003 -8.3195684e-004 5.185089469 2.185214371
5 5. 4.77241325e-003 -5.06292072e-002 -1.94899105e-003 3.13229044e-002
6 6. 5.05900494e-002 -2.53490478e-002 2.00916689e-002 3.67559114e-002
7 7. -4.03850328e-003 5.24337929e-003 -5.280406982 5.280571362
8 8. -1.38934723e-003 3.12915198e-003 -2.274678171 2.274854119
Could you help me with it? Below is an example of script which reads it, but incorrently and I was not able to correct it.
d=dir('*.txt'); % all .txt files in working directory
N=length(d) % how many did we find?
A=zeros(24,N); % allocate for 24 points of data 1 column/file
for k=1:N
[fid,msg]=fopen(d(k).name,'r'); % open for read permission
if fid<0, error(['Failed fopen: ' d(k).name msg]),end % check opened it
A(:,k)=cell2mat(textscan(fid, '%*d %f', ...
'headerlines',1, 'collectoutput', 1));
fid=fclose(fid);
end
;
A
Accepted Answer
More Answers (0)
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!