Enter values into x array and y array

1 回表示 (過去 30 日間)
Jovos
Jovos 2016 年 2 月 20 日
回答済み: Walter Roberson 2016 年 2 月 20 日
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 Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by