Enter values into x array and y array

1 次查看(过去 30 天)
Hi, I have sorted the data out of the txt file. I wonder how could I enter the year and dat into an x and y array respectively so that I can plot (x,y). Thank you.
filename = input('Please enter the file name: ');
fid01 = fopen(filename,'r');
for i= 1:15
line = fgets(fid01);
end
for i = 16:66
line = fgets(fid01);
z = strread(line,'%s');
year = str2num(z{1});
for j = 2:13
dat = str2num(z{j});
if ( dat== -99.9900)
dat = NaN;
end
year = year + (j-1)/12 ;
x = year;
y = dat;
fprintf('%f %f\n',x,y);
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-20
fmt = repmat('%f',1,14);
fid = fopen('CO2.txt','rt');
data = textscan(fid, fmt, 'HeaderLines', 15, 'CollectOutput', 1);
fclose(fid);
x = bsxfun(@plus, data{1}(:,1), (0:11)/12);
y = data{1}(:,2:13);
y(y<0) = nan;
plot(x, y);
legend({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'});

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by