Read text file without header and separate columns

207 views (last 30 days)
Dear Matlab Team,
my problem is as follows. I could not find a solution in other forums, but none seem to work for my issue:
I have a text file with a big headliner, which I want to skip. The data I need starts in line 194 and has 50.000 values. There are 3 values in each row, sperated by ",". Basically, what I need is to extract the 50 000 values and put it in matrix with 3 columns for further analyzation.
This is how the file looks like (starting from below "DATA BLOCK..."]
[DATA BLOCK 1-1-1]
How can I separate this data and create a matrix from it?
Thanks in advance,

Accepted Answer

Rik on 29 Apr 2020
Edited: Rik on 29 Apr 2020
You can use the readmatrix function.
A = readmatrix(filename,'NumHeaderLines',193);
As Walter suggested below: releases older than R2019a are missing the readmatrix function. On R2013b or newer, the readtable function can help out:
A = table2array(readtable(filename,'NumHeaderLines',193, 'readvariablenames', false));

Sign in to comment.

More Answers (1)

B on 21 Apr 2022
Edited: Rik on 21 Apr 2022
I think summarizing you've got something like this:
input_folder = 'folder';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {});
nfile = numel(file_paths);
for i = 1 : nfile
data= readmatrix(file_paths{i},'NumHeaderLines',1);% a display of data in a single file
A(:,cols)=data; % A is your desired result outpout file


Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!