Loading data from a dat file containing header

25 ビュー (過去 30 日間)
RS
RS 2014 年 12 月 27 日
コメント済み: Star Strider 2018 年 6 月 28 日
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 日
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 件のコメント
Margaret Shanafield
Margaret Shanafield 2018 年 6 月 28 日
So nice to see an explanation with the answer!!
Star Strider
Star Strider 2018 年 6 月 28 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 12 月 27 日
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 日
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.
Geoff Hayes
Geoff Hayes 2014 年 12 月 28 日
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!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by