Effacer les filtres
Effacer les filtres

Enter values into x array and y array

1 vue (au cours des 30 derniers jours)
Jovos
Jovos le 20 Fév 2016
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

Réponses (1)

Walter Roberson
Walter Roberson le 20 Fév 2016
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'});

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by