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

40 visualizaciones (últimos 30 días)
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..

Respuesta aceptada

Stephen23
Stephen23 el 24 de Sept. de 2021
Editada: Stephen23 el 24 de Sept. de 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
  2 comentarios
Zoltan Varallyay
Zoltan Varallyay el 2 de En. de 2024
I tried this code but the lines with setvartype provide error:
Error using matlab.io.ImportOptions/getNumericSelection
Unknown variable name: 'T_start'.
Error in matlab.io.ImportOptions/setvartype (line 325)
selection = opts.getNumericSelection(selection);
Error in ReadDatFile (line 32)
opts = setvartype(opts,'T_start','duration');
Stephen23
Stephen23 el 2 de En. de 2024
"I tried this code but the lines with setvartype provide error... Unknown variable name: 'T_start'."
Because unlike the OP your data file does not contain a column named "T_start", or has a different format.
Of course you would only try to access a column that exists in your data file. Trying to access a column from someone else's data file that you probably are not using and is nothing like your file format... is unlikely to be successful.
Given that this code was written to import a very specific file format (and the OP did not tell us what created it), have you checked that your file has exactly the same format?
If you want further help please upload a sample data file by clicking the paperclip button.

Iniciar sesión para comentar.

Más respuestas (1)

Andres
Andres el 24 de Sept. de 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 '}});

Categorías

Más información sobre Data Type Conversion 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