Loading a dat file in Matlab

2 次查看(过去 30 天)
Jason
Jason 2014-10-26
回答: Star Strider 2014-10-26
In my "global_v1" mathlab script, I'm trying to load data from the file "GlobalTemp_v2.dat" I am interested in column 14 (the temperature) and column 1 (the year). When I run the script, the 14th column of "data1" only contains three NaN values. How do I load the data? Thanks.
This is the link to my DAT file: https://www.dropbox.com/s/77qyr6vhg4ahgs2/GlobalTemp_v2.dat?dl=0
Here is my code:
fid1 = fopen('GlobalTemp_v2.dat'); % have 20 columns
format = '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f ';
data1 = textscan(fid1, format, 'HeaderLines', 6,'delimiter',' ');

回答(1 个)

Star Strider
Star Strider 2014-10-26
There were a couple small problems in your code. First, there are 8 header lines, and the appropriate delimiter may be '\n'.
This works:
fid1 = fopen('GlobalTemp_v2.dat'); % have 20 columns
data1 = textscan(fid1, repmat('%f ', 1, 20), 'HeaderLines',8, 'Delimiter','\n');
Date = data1{1}; % Date (Calendar Years)
Temp = data1{14}; % Temperature
figure(1)
plot(Date, Temp)
grid
xlabel('Year')
ylabel('T (°F)')
The plot is just for fun for me, since I wanted to see what the data look like:

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by