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);
採用された回答
その他の回答 (1 件)
Image Analyst
2020 年 3 月 15 日
編集済み: Image Analyst
2020 年 3 月 15 日
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!