Info

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

Importing data from text file

2 views (last 30 days)
Henrik
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
alldata=fileread(filename);
and then find the individual files by searching for a string that denotes the start of a new file:
start_index=strfind(alldata,startstring);
end_index=strfind(alldata,endstring);
this_file=alldata(start_index:end_index);
What I get out of this is a 1xn character string. When I do
this_file
In the MATLAB window, I get the output shown above. This is where I got stuck.
EDIT:
I found a solution. In short, I used
comment_indices=strfind(data_file,'#');
to remove the first lines of comments
lineshift_indices=strfind(data_file,sprintf('\n'));
to find where the actual data starts
header_names=data_file(1:lineshift_indices(1));
header_names=strsplit(header_names);
to find the header names
and then
H_ind=find(ismember(header_names,'H'));
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
www.mathworks.com/matlabcentral/fileexchange/18430
for creating matrix A containing your data columns.
Good luck!
Mira
  1 Comment
Henrik
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.
Henrik

This question is closed.

Community Treasure Hunt

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

Start Hunting!