フィルターのクリア

reading data, loop and plot.

1 回表示 (過去 30 日間)
Jovos
Jovos 2016 年 2 月 20 日
編集済み: Walter Roberson 2016 年 2 月 20 日
I'm trying to get the year and CO2 levels from a txt file, plot them on a x-y graph. I have a problem with my loop.The -99.99 values are bad statistics that I try to remove. My x and y are just one value, respectively. Shouldn't they be two arrays??
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
monthly_entry = str2num(z{j});
if (monthly_entry == -99.99)
monthly_entry = [ ];
end
x = year + 1/12;
y = monthly_entry;
end
end
plot(x,y);

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 20 日
No, they should be single values. You are writing over all of the variables each iteration of your "for j"
x = year + 1/2;
overwrites all of x, leaving x of whatever length "year" happens to be.
More typical would be to store into something indexed by your loop variables, such as
x(i, j) = year + 1/2;
y(i, j-1) = monthly_entry;
However, you set monthly_entry to [], which is length 0, for data that is bit-for-bit identical to whatever MATLAB happens to interpret -99.99 as being this time around, a match that would be rare but would occur by accident from time to time. (I already told you about this code being incorrect.) If that ever does happen, you would be unable to store the length-0 [] into the length-1 y(i,j-1)
Your handling of bad data is not well defined. I would suggest to you that your handling would be considerably better defined if you used NaN or 0 for those cases instead of trying to leave a hole in the data.
  4 件のコメント
Walter Roberson
Walter Roberson 2016 年 2 月 20 日
編集済み: Walter Roberson 2016 年 2 月 20 日
You seem to be doing a lot of unnecessary work.
fmt = repmat('%f',1,14);
fid = fopen('CO2.txt','rt');
data = textscan(fid, fmt, 'HeaderLines', 15, 'CollectOutput', 1);
fclose(fid);
x = data{1}(:,1);
y = data{1}(:,2:end);
plot(x, y);
legend({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Avg'});
Jovos
Jovos 2016 年 2 月 20 日
編集済み: Jovos 2016 年 2 月 20 日
So now I have all the values, how could I arrange these values into a X array? SO that I can plot it?
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
end
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by