MATLAB Answers


This question is closed. Reopen it to edit or answer.

Importing data from text file

2 views (last 30 days)
Henrik on 5 Dec 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
I need help reading a number of data files. An example of how the files look is the following:
# some header text and numbers
# there are around 15 lines of this, the number of lines changes
# H K L Delta more names
1 2 90 22
2 4 5 9
1 8 7 2
I need to get the columns of H, K, L and a few others out of this file. The problem is that the number of header lines changes, the number of columns changes, the order of the parameters in the columns changes, and the length of the columns change.
So another file could be
# some different header text and numbers
# here are some numbers
# there are around 15 lines of this, the number of lines changes
# Delta L H K Delta more names
4 2 9 40
2 4 5 9
To complicate things, all my data files are in one big text file. I have read it using
and then find the individual files by searching for a string that denotes the start of a new file:
What I get out of this is a 1xn character string. When I do
In the MATLAB window, I get the output shown above. This is where I got stuck.
I found a solution. In short, I used
to remove the first lines of comments
to find where the actual data starts
to find the header names
and then
to find the column for each header.
It works as intended, but if anyone has suggestions for improvements, I'd be happy to see them

Answers (1)

Miroslav Balda
Miroslav Balda on 5 Dec 2014
You have preprocessed the problem rather far. Store 'the_file' in the text file, say 'data.txt' and then use the function txt2mat
for creating matrix A containing your data columns.
Good luck!
  1 Comment
Henrik on 5 Dec 2014
Hi Mira
Thanks for the answer. I didn't like the idea of having to save the data in another file, so I tried some more Googling etc. My solution is not pretty, but it works. I'll edit it into my question.

Community Treasure Hunt

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

Start Hunting!

Translated by