Reading data of nonstandard format
2 views (last 30 days)
Show older comments
Mohammad Farhat
on 24 Sep 2021
Commented: Star Strider
on 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
0 Comments
Accepted Answer
Star Strider
on 24 Sep 2021
I use textscan for these problems.
Try this —
fidi = fopen('reconstructed[1].txt','rt')
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
D
D_Check_1 = D{1}(1:5,:) % First 5 Rows
D_Check_3 = D{3}(1:5,:) % First 5 Rows
Out = cell2mat(D)
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 Comments
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!