Enter values into x array and y array
Show older comments
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
Answers (1)
Walter Roberson
on 20 Feb 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'});
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!