Reading mixed format data containing both text and numbers from a '.txt' file in matlab
Afficher commentaires plus anciens
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..
Réponse acceptée
Plus de réponses (1)
Andres
le 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 '}});

Catégories
En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!