Loading data from a dat file containing header

Hi
I have data file (KEmodel12_G201.dat) in format as seen in attached image. While loading this data in matlab, it shows error that 1st and 2nd lines do not match. First line is the header, required when i am using this data for tecplot. If remove the header, it works fine.
How do i make matlab read it from the 2nd line and not consider the 1st line? I have also attached the data file in .txt format as .dat is not supported here.
I had seen similar post, but none actually answered the question i have. This is the code i am using to load the data.
modeldata = modeldata('KEmodel12_G201.dat');
x = modeldata(:,1);
k = modeldata(:,7);
e = modeldata(:,8);
Thanks Rachit
&nbsp

 채택된 답변

Star Strider
Star Strider 2014년 12월 27일

4 개 추천

Try this:
fidi = fopen('KEmodel12_G201.txt');
d = textscan(fidi, '%f%f%f%f%f%f%f%f', 'HeaderLines',1, 'Delimiter','\n', 'CollectOutput',1);
fclose(fidi);
modeldata = cell2mat(d);

댓글 수: 6

RS
RS 2014년 12월 27일
Hi Star Strider,
Thank-you so much. Its working. It would be good if you can briefly explain how the code works.
Thanks Rachit.
Hi Rachit,
My pleasure!
Line by line:
First ‘fidi’ is the file identifier created by fopen.
The textscan call goes to the file, reads the 8 columns of data in floating-point format (the 8 ‘%f’ edit descriptors) skipping the one 'HeaderLine', using the ‘newline’ character '\n' to determine where to break the line and start reading a new line, and I set 'CollectOutput' to 'true' (or equivalently 1), so that it would read the data in as a single (Nx8) cell rather than eight individual cells.
The fclose line closes the file.
The last line calls the cell2mat function to convert the cell array ‘d’ to an (Nx8) double array so you can use it in your calculations, since code code you already wrote is intended for an (Nx8) double matrix.
(Cell arrays are great for storing and manipulating data, but they require extra effort to do calculations with them.)
Have fun!
‘Star Strider’
RS
RS 2014년 12월 27일
Nice explanation. Thanks for writing and sharing your knowledge!!
Rachit.
Thank you.
My pleasure!
That’s the reason MATLAB Answers exists. It’s also fun for me.
‘Star Strider’
So nice to see an explanation with the answer!!
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 12월 27일

2 개 추천

Rachit - consider using importdata to read the data from file so that the first header line is ignored. Try something like
A = importdata('myFile.txt', '\t', 1);
In the above, we specify which file to import the data from, the (tab) delimiter between each column, and the number of header lines to ignore.

댓글 수: 4

RS
RS 2014년 12월 27일
편집: RS 2014년 12월 27일
Hi Geoff Hayes,
Thanks for the answer. I tried, it is showing following error:
"Index exceeds matrix dimensions.
Error in kepsratio (line 3) k = A(:,7);"
I think it would work if there were 8 headers in first line separated by a delimiter (tab in this case).
It does not show error for loading the 1st column which is x (line 2 in the code). Also if i type x in command window it shows following value for x:
x =
'ZONE T="Inviscid"'
Whereas actually it should be all values in the 1st column (-7.64 ......).
I have attached the data file and the 4 line code, if you want to try it with the data file.
Thanks.
Geoff Hayes
Geoff Hayes 2014년 12월 27일
편집: Geoff Hayes 2014년 12월 27일
Rachit - if you read the documentation for the importdata function, and consider the examples, you will note that A is a structure with the text/header info and numeric data fields. So you could try
x = A.data(:,1);
k = A.data(:,7);
e = A.data(:,8);
to get the desired numeric data.
See importdata output argument for more details.
RS
RS 2014년 12월 27일
Hi Geoff Hayes,
I tried what u suggested, it showed following error:
"Attempt to reference field of non-structure array." at line 2 i.e in x = A.data(:,1);.
Answer suggested by Star Strider is working. However, it is good to know possible ways to get a solution.
Thank-you for the suggestions,
Rachit.
This is what happens when I try to answer a question without using MATLAB! :)
The delimiter between each column should be a single whitespace. So the following
A = importdata('KEmodel12_G201.txt', ' ', 1);
would return
A =
data: [201x8 double]
textdata: {'ZONE T="Inviscid"'}
And so now you could extract the data as
x = A.data(:,1);
k = A.data(:,7);
e = A.data(:,8);
Sorry for the confusion!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

RS
2014년 12월 27일

댓글:

2018년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by