Reading Text file with headers

116 visualizaciones (últimos 30 días)
Kabit Kishore
Kabit Kishore el 15 de Mzo. de 2020
Comentada: Star Strider el 15 de Mzo. de 2020
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);

Respuesta aceptada

Star Strider
Star Strider el 15 de Mzo. de 2020
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 comentarios
Kabit Kishore
Kabit Kishore el 15 de Mzo. de 2020
Thanks alot.
Star Strider
Star Strider el 15 de Mzo. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 15 de Mzo. de 2020
Editada: Image Analyst el 15 de Mzo. de 2020
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');

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by