Reading mixed format data containing both text and numbers from a '.txt' file in matlab
33 次查看(过去 30 天)
显示 更早的评论
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..
0 个评论
采纳的回答
Stephen23
2021-9-24
编辑:Stephen23
2021-9-24
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
2 个评论
Zoltan Varallyay
2024-1-2
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
2024-1-2
"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.
更多回答(1 个)
Andres
2021-9-24
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 '}});
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!