Reading data of nonstandard format

3 Ansichten (letzte 30 Tage)
Mohammad Farhat
Mohammad Farhat am 24 Sep. 2021
Kommentiert: Star Strider am 24 Sep. 2021
Hello,
The data file I'm trying to read contains text headers separating two column matricies that I want to save separately.
Each header is over several lines (different number for each header), all header lines start with ">", then I have the two column matrix space separated.
a reduced sample of this file is attached. Please let me know how to read it and save the matricies separately.
Thanks

Akzeptierte Antwort

Star Strider
Star Strider am 24 Sep. 2021
I use textscan for these problems.
Try this —
fidi = fopen('reconstructed[1].txt','rt')
fidi = 3
k1 = 1;
while ~feof(fidi)
HL = 14*(k1==1) + 10*(k1>1);
C = textscan(fidi, '%f%f', 'HeaderLines',HL, 'CollectOutput',true);
M = cell2mat(C);
if isempty(M) % Empty Matrix Indicates End-Of-File
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
k1 = 2
k1 = 3
k1 = 4
D
D = 3×1 cell array
{50×2 double} {42×2 double} {92×2 double}
D_Check_1 = D{1}(1:5,:) % First 5 Rows
D_Check_1 = 5×2
112.0797 39.2348 113.0445 39.8864 113.4530 40.1759 114.5988 40.7504 115.0607 41.0647
D_Check_3 = D{3}(1:5,:) % First 5 Rows
D_Check_3 = 5×2
130.4700 41.9900 130.2100 41.8100 129.9939 41.5588 129.8994 41.3697 129.8816 41.2835
Out = cell2mat(D)
Out = 184×2
112.0797 39.2348 113.0445 39.8864 113.4530 40.1759 114.5988 40.7504 115.0607 41.0647 115.3221 41.1908 115.8189 41.5431 116.0390 41.7845 116.3340 42.2555 116.4687 42.3217
The different segments import correctly. The rest of the file should as well, so long as the file format and separating lines remain the same. If those change, it will be necessary for you to tweak the code to adapt it to them.
Here, ‘D’ has the individual matrices, and ‘Out’ has them vertically concatenated.
.
  3 Kommentare
Mohammad Farhat
Mohammad Farhat am 24 Sep. 2021
Fixed. Works, thanks!
Star Strider
Star Strider am 24 Sep. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by