Extracting the daily time series from a text file to a single column

1 回表示 (過去 30 日間)
Shahram Sahraei
Shahram Sahraei 2019 年 7 月 23 日
コメント済み: Shahram Sahraei 2019 年 7 月 24 日
Dear Matlab users,
I have a text file (attached here) containing daily time series from 2013 to 2019 and I want to take out the daily data points and save it in a column vector in chronological order. I would be really grateful of your help in this regard.
Best,
Shahram
  2 件のコメント
Guillaume
Guillaume 2019 年 7 月 23 日
It would be a lot more useful if you attached a representative text file.
Shahram Sahraei
Shahram Sahraei 2019 年 7 月 23 日
Dear Guillaume,
Thanks for you suggestion. I just modified my question and attached the text file to it.

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

採用された回答

David Wilson
David Wilson 2019 年 7 月 24 日
A quick hack is:
fname = 'D59.txt'
Flow = [];
fid=fopen(fname);
Yr = 2012;
R = [];
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
s = strtrim(tline);
disp(s)
if strncmp(s,'Day',3)
disp(s)
Yr = Yr+1;
X = [];
i=1;
while i < 32
s = deblank(fgetl(fid));
if isempty(s)
s = deblank(fgetl(fid));
end
%disp(s);
x = str2num(s); disp(x)
n = length(x);
if n == 8
x = [x(1:2), NaN, x(3), NaN, x(4), NaN, x(5:6), NaN, x(7), NaN x(8)];
elseif n == 12
x = [x(1:2), NaN, x(3:end)];
end
X = [X;x];
i=i+1;
end % for
Rdat = X(:,[2:end]);
r = Rdat(:);
r(find(isnan(r)))=[]; % drop bad days (leap year?)
R = [R; r];
end
end
fclose(fid);
plot(R)
  1 件のコメント
Shahram Sahraei
Shahram Sahraei 2019 年 7 月 24 日
Dear David,
Thank you very much for your answer. That helps me a lot.
Best,
Shahram

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by