Reading Text file with headers

Hi
I have a text file that I want to read and organize it in Matlab. In the text file there are many headers and under these header there are many attributes. So what i want to do is i want to create a table. I have attached my Text file (X.txt) and a sample file that shows how the data should be in its final form. This is what i have tried at the moment. I dont know to progress further. Any help would be great. Thank you in advance.
Clear all; close all;clc;
fid=fopen(X.txt, 'r');
fclose(fid);
A=fileread(X.txt);

 采纳的回答

Another approach:
txt = fileread('X.txt');
timepos = strfind(txt,'Time:');
endpos = strfind(txt, 'end');
for k = 1:numel(timepos)
section{k,:} = txt(timepos(k):endpos(k));
end
for k = 1:numel(section)
tv(k,:) = sscanf(section{k}, 'Time: %f');
Inc = textscan(section{k}, ['%*d %f %f %f' repmat('%*f', 1, 7)], 'HeaderLines',5, 'EndOfLine','\r\n', 'CollectOutput',1);
Inn = cell2mat(Inc);
RowIdx = find(Inn(:,1) == -50);
Data(k,:) = Inn(RowIdx,:);
end
Results = table(tv,Data(:,1),Data(:,2),Data(:,3), 'VariableNames',{'Time','Depth','Head','Moisture'})
producing:
Results =
32×4 table
Time Depth Head Moisture
______ _____ _______ ________
0 -50 -492 0.2184
0.9677 -50 -520.16 0.2171
1.9355 -50 -541.35 0.2161
2.9032 -50 -562.84 0.2152
3.871 -50 -586.34 0.2141
4.8387 -50 -587.82 0.214
5.8064 -50 -521.24 0.2171
6.7742 -50 -550.97 0.2156
7.7419 -50 -587.73 0.214
8.7097 -50 -585.08 0.2141
9.6774 -50 -627.49 0.2124
10.645 -50 -690.44 0.2101
11.613 -50 -743.11 0.2081
12.581 -50 -793.24 0.2065
13.548 -50 -847.58 0.2048
14.516 -50 -902.25 0.2032
15.484 -50 -966.9 0.2013
16.452 -50 -1044.4 0.1994
17.419 -50 -1110.9 0.1978
18.387 -50 -1008.6 0.2004
19.355 -50 -962.21 0.2015
20.323 -50 -1072.7 0.1987
21.29 -50 -1160.6 0.1966
22.258 -50 -1255.1 0.1946
23.226 -50 -1379.7 0.1922
24.194 -50 -1512.8 0.1896
25.161 -50 -1690.8 0.1869
26.129 -50 -1885 0.184
27.097 -50 -2119.9 0.1811
28.064 -50 -2376.2 0.1781
29.032 -50 -2798.5 0.1741
30 -50 -3290.3 0.1701
This is reasonably fast, and it produces the desired output.

4 个评论

Hi. Thank you it works perfectly. One more thing what if i want to create multiple table like above for the depth values from (0 to -50). Thank you once again.
As always, my pleasure!
That requires some minor modifications to my code:
txt = fileread('X.txt');
timepos = strfind(txt,'Time:');
endpos = strfind(txt, 'end');
for k = 1:numel(timepos)
section{k,:} = txt(timepos(k):endpos(k));
end
Deep = 0:-10:-50;
for k1 = 1:numel(section)
for k2 = 1:numel(Deep)
tv(k1,:) = sscanf(section{k1}, 'Time: %f');
Inc = textscan(section{k1}, ['%*d %f %f %f' repmat('%*f', 1, 7)], 'HeaderLines',5, 'EndOfLine','\r\n', 'CollectOutput',1);
Inn = cell2mat(Inc);
RowIdx = find(Inn(:,1) == Deep(k2));
Data{k1,k2}= Inn(RowIdx,:);
end
end
for k2 = 1:numel(Deep)
Datam = cell2mat(Data(:,k2));
Results{k2} = table(tv,Datam(:,1),Datam(:,2),Datam(:,3), 'VariableNames',{'Time','Depth','Head','Moisture'});
end
For example:
Depth_00 = Results{1}
Depth_30 = Results{4}
produce:
Depth_00 =
32×4 table
Time Depth Head Moisture
______ _____ _______ ________
0 0 -492 0.2184
0.9677 0 -522.56 0.217
1.9355 0 -544.1 0.216
2.9032 0 -565.87 0.215
3.871 0 -591.21 0.2139
4.8387 0 -588.11 0.214
5.8064 0 -512.63 0.2175
6.7742 0 -555.69 0.2154
7.7419 0 -593.9 0.2137
8.7097 0 -584.04 0.2142
9.6774 0 -636.82 0.2121
10.645 0 -702 0.2096
11.613 0 -753.68 0.2076
12.581 0 -804.86 0.2061
13.548 0 -860.55 0.2044
14.516 0 -917.2 0.2027
15.484 0 -987.61 0.2008
16.452 0 -1068.4 0.1988
17.419 0 -1131.6 0.1973
18.387 0 -966.41 0.2016
19.355 0 -977.92 0.2011
20.323 0 -1099.3 0.1981
21.29 0 -1193 0.1958
22.258 0 -1293.5 0.1938
23.226 0 -1433.7 0.1911
24.194 0 -1582.3 0.1886
25.161 0 -1789 0.1854
26.129 0 -2016.6 0.1823
27.097 0 -2292.8 0.1791
28.064 0 -2609.8 0.1759
29.032 0 -3224.6 0.1706
30 0 -3926.2 0.1657
Depth_30 =
32×4 table
Time Depth Head Moisture
______ _____ _______ ________
0 -30 -492 0.2184
0.9677 -30 -521.05 0.2171
1.9355 -30 -542.38 0.2161
2.9032 -30 -563.97 0.2151
3.871 -30 -588.11 0.214
4.8387 -30 -587.92 0.214
5.8064 -30 -517.93 0.2172
6.7742 -30 -552.7 0.2155
7.7419 -30 -590.03 0.2139
8.7097 -30 -584.69 0.2142
9.6774 -30 -630.92 0.2123
10.645 -30 -694.75 0.2099
11.613 -30 -747.1 0.2079
12.581 -30 -797.61 0.2063
13.548 -30 -852.44 0.2047
14.516 -30 -907.83 0.203
15.484 -30 -974.63 0.2011
16.452 -30 -1053.3 0.1992
17.419 -30 -1118.7 0.1976
18.387 -30 -993.28 0.2008
19.355 -30 -966.72 0.2013
20.323 -30 -1082.6 0.1985
21.29 -30 -1172.5 0.1963
22.258 -30 -1269.4 0.1943
23.226 -30 -1399.4 0.1918
24.194 -30 -1538.5 0.1892
25.161 -30 -1726.3 0.1864
26.129 -30 -1932.6 0.1833
27.097 -30 -2181.4 0.1804
28.064 -30 -2459.5 0.1772
29.032 -30 -2938.6 0.1728
30 -30 -3498 0.1687
As always, my pleasure!

请先登录,再进行评论。

更多回答(1 个)

This will do work for the attached file:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% % Have user browse for a file, from a specified "starting folder."
% % For convenience in browsing, set a starting folder from which to browse.
% startingFolder = pwd; % or 'C:\wherever';
% if ~exist(startingFolder, 'dir')
% % If that folder doesn't exist, just start in the current folder.
% startingFolder = pwd;
% end
% % Get the name of the file that the user wants to use.
% defaultFileName = fullfile(startingFolder, 'x*.txt');
% [baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
% if baseFileName == 0
% % User clicked the Cancel button.
% return;
% end
% fullFileName = fullfile(folder, baseFileName)
fullFileName = fullfile(pwd, 'x.txt')
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
% Initialize an empty table.
z = zeros(10000, 1); % Say, 10 thousand rows. We'll crop later if that's too many.
t = table(z, z, z, z, z, z, z, z, z, z, z, z, ...
'VariableNames', {'TimePoint', 'Node', 'Depth', 'HeadMoistureL', 'HeadMoisture', 'K', 'C', 'Flux', 'Sink', 'Kappa', 'v_KsTop', 'Temp'});
row = 0;
while ischar(textLine)
% Print out what line we're operating on.
% fprintf('%s\n', textLine);
if contains(textLine, 'Time', 'IgnoreCase', true)
TimePoint = str2double(textLine(8:end));
end
if contains(textLine, 'node', 'IgnoreCase', true)
% Found the header line.
% Read the next 2 lines and throw them away.
textLine = fgetl(fileID);
textLine = fgetl(fileID);
% Now read 101 lines
for k = 1 : 101
textLine = fgetl(fileID);
numbers = sscanf(textLine, '%f ');
t(row + k, :) = array2table([TimePoint, numbers']);
end
% Increment the row
row = row + 101;
fprintf('Imported %d rows so far.\n', row);
end
% Read the next line.
textLine = fgetl(fileID);
end
% Crop to however many rows we actually ended up using.
t = t(1:row, :);
% All done reading all lines, so close the file.
fclose(fileID);
fprintf('All done!\n');

类别

帮助中心File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by