Read multiple txt files and save them according to their name
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mathew Smith
am 30 Aug. 2023
Bearbeitet: Mathieu NOE
am 31 Aug. 2023
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
0 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 30 Aug. 2023
hello
maybe this is what you want
A is now a cell array of structure that you can further expand if you need , for the time being you get the info of what txt file it's related and the data
for example here we have one file so
>> A{1}
struct with fields:
name: 'A16.txt'
data: [8×6 double]
and the data :
>> A{1}.datas =
1.0000 1.0000 -0.0206 0.0953 -0.0501 0.0968
2.0000 2.0000 -0.0098 0.0006 0.0009 0.0513
3.0000 3.0000 0.0060 -0.0084 6.1690 4.1692
4.0000 4.0000 0.0028 -0.0008 5.1851 2.1852
5.0000 5.0000 0.0048 -0.0506 -0.0019 0.0313
6.0000 6.0000 0.0506 -0.0253 0.0201 0.0368
7.0000 7.0000 -0.0040 0.0052 -5.2804 5.2806
8.0000 8.0000 -0.0014 0.0031 -2.2747 2.2749
d = dir('*.txt'); % all .txt files in working directory
N = length(d) % how many did we find?
for k=1:N
filename = d(k).name;
A{k}.name = filename;
A{k}.data =readmatrix(filename);
end
A{1}
4 Kommentare
Stephen23
am 30 Aug. 2023
Bearbeitet: Stephen23
am 30 Aug. 2023
Rather than creating an inefficient cell array of scalar structures, you can simply use the structure array which already exists in your code:
d = dir('*.txt'); % d is a structure array
for k = 1:numel(d)
d(k).data =readmatrix(d(k).name);
end
This is more robust, uses less memory, creates fewer variables, no data duplication, and is more versatile:
Mathieu NOE
am 31 Aug. 2023
Bearbeitet: Mathieu NOE
am 31 Aug. 2023
yes indeed ! my brain wasn't working well that day
you're always right !
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!