Reading data of nonstandard format
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2021-9-24
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!