how to import a file in MATLAB?

I want to import a file (.dat or txt) in MATLAB, which contains numbers and NaN in some columns.
My file looks like this:
Name:
Day: 5/07/21998
UT LT hpp fpH SSP EF GG
0.0808 21.0808 227 - - N
0.1732 21.1732 300 4.7 340 - N
0.2533 21.2533 237 4.6 335 - N
0.3333 21.3333 249 - - N
0.4233 21.4233 251 - - N
0.5033 21.5033 244 4.6 328 - N
0.5833 21.5833 256 - - N
0.6733 21.6733 253 4.7 330 - N

2 件のコメント

Walter Roberson
Walter Roberson 2013 年 10 月 3 日
Do any values before the UT heading need to be imported? Which of the columns do you want read in? For example the EF column is shown here as always "-", but should that be skipped or read in? In the SSF column, should the blank entries be important as 0 or as NaN or should the column be imported as string without numeric interpretation?
Jesus
Jesus 2013 年 10 月 3 日
編集済み: Jesus 2013 年 10 月 3 日
1- No. The data before UT does not need to be imported.
2- I'd like to read all the columns (UT, LT, hpp, fph, SSP, EF, GG). UT( float), LT (float), hpp(float), fpH(float), SSP(float), EF(string or char), GG (string or char).
Note: each column is separated by tab

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

 採用された回答

Nishitha Ayyalapu
Nishitha Ayyalapu 2013 年 10 月 14 日
編集済み: Nishitha Ayyalapu 2013 年 10 月 14 日

2 投票

You could use
readtable
Lets say your data is in testdata.txt then following would create a table "T" with 7 columns, 8 rows and also saves column headers (UT, LT, hpp..etc).
to access UT column you would do
T.UT
However, with this approach fpH to hold all numeric data as float, the '-' entries are treated as empty and are recorded as nan.
T = readtable('testdata.txt','Delimiter','\t','HeaderLines',3,...
'TreatAsEmpty','-','Format','%f%f%f%f%f%s%s');
If fpH has to hold '-' entries and also the numeric data, fpH can be read as a string. But all the numeric data is also recorded as string
T = readtable('testdata.txt','Delimiter','\t','HeaderLines',3,...
'Format','%f%f%f%s%f%s%s');

3 件のコメント

Jesus
Jesus 2013 年 10 月 15 日
I am using MATLAB 7.3.0 (R2006b). I tried what is written in the answer, but it did not work. Appeared the following message: ??? Undefined function or method 'readtable' for input arguments of type 'char'.
Walter Roberson
Walter Roberson 2013 年 10 月 15 日
編集済み: Walter Roberson 2013 年 10 月 15 日
readtable() is very new. You can convert Nishitha's code as:
fid = fopen('testdata.txt', 'r');
T = textscan(fid, '%f%f%f%f%f%s%s', 'Delimiter','\t','HeaderLines',3,...
'TreatAsEmpty','-');
fclose(fid);
then the various columns are T{1}, T{2} and so on. But HeaderLines is perhaps 5 rather than 3.
Jesus
Jesus 2013 年 10 月 15 日
Nishitha Ayyalapu and Walter Roberson, thank you. The TextScan worked perfectly in MatLab (R2006b).

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

その他の回答 (1 件)

Gautam PAL
Gautam PAL 2013 年 10 月 16 日

0 投票

How to read video file using matlab..?

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

質問済み:

2013 年 10 月 3 日

回答済み:

2013 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by