Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Importing data from text file

조회 수: 2 (최근 30일)
Henrik
Henrik 2014년 12월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (1개)

Miroslav Balda
Miroslav Balda 2014년 12월 5일
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
Henrik
Henrik 2014년 12월 5일
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

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by