Reading mixed format data containing both text and numbers from a '.txt' file in matlab

23 views (last 30 days)
So I've ha a txt file that starts off with a few lines of text contianing both characters and numbers e.g. seen in text.txt, so I only included one row of data, but it has over 600 rows, which is the data I want to work with.
I have a hard time to finding a way to read/open/load the file and to seperate the unnecesary information from the data that I want to work with. Any tips on how to move forward?
[filename path] = uigetfile([files.path '*.txt; *.inp'],'Choose file');
data = load_data(filename);
So I tried with
function [data] = load_data(filename)
S = fileread(filename);
lines = cellfun(@strtrim, regexp(S, '\r?\n', 'split'),'UniformOutput', 0);
values = cellfun(@(s) cell2mat(textscan(s, '')), lines, 'uniform', 0);
mask = cellfun(@isempty, values);
values(mask) = lines(mask);
data = values(mask);
end
Since I have no idea how to start, and it obviously doesn't work..

Accepted Answer

Stephen23
Stephen23 on 24 Sep 2021
Edited: Stephen23 on 24 Sep 2021
fpt = '.'; % absolute or relative path to where the file is saved.
fnm = fullfile(fpt,'text.txt');
% Count the header lines:
[fid,msg] = fopen(fnm,'rt');
assert(fid>0,'%s',msg)
fgetl(fid); % first line
cnt = 2;
while contains(fgetl(fid),':')
cnt = cnt+1;
end
fclose(fid);
% Import the data:
opts = detectImportOptions(fnm,...
'FileType','delimited',...
'Delimiter','\t',...
'ConsecutiveDelimitersRule','join',...
'NumVariables',11,...
'NumHeaderLines',cnt,...
'VariableNamesLine',cnt+1,...
'VariableNamingRule','preserve',...
'DecimalSeparator',',');
opts = setvartype(opts,'T_start','duration');
opts = setvartype(opts,'Date','datetime');
opts = setvaropts(opts,'Date','InputFormat','dd,MM,uuuu');
T = readtable(fnm, opts);
T.Date.Format = 'uuuu-MM-dd' % ISO 8601 datestamp
T = 1×11 table
Date T_start A B C D E F G H I __________ ________ _ _____ ____ ______ ______ ______ ______ ______ ______ 2021-06-29 16:04:16 0 23909 7155 393.37 638.68 403.87 669.19 403.87 669.19

More Answers (1)

Andres
Andres on 24 Sep 2021
Less textbook, but as you are only interested in the numeric data rows, you may use txt2mat from the file exchange,
% ffn = 'C:\...\text.txt';
D = txt2mat(ffn,'ReplaceRegExpr',{{'[,:](\d+)[,:]',' $1 '}});

Community Treasure Hunt

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

Start Hunting!

Translated by