How can I get and write data from text file?

Text file as follows:
  • A column: Year (1998:1:2017)
  • B column: Day of the year (1:1:365 or 366)
  • C column: Hour
1998 152 1 30 25 12.5
1998 152 1 30 30 12
1998 152 1 30 35 11.8
1998 152 1 30 40 11.9
1998 152 1 30 45 12
I would like to get data but I have problem for leap year of 366 days. I'd like to extract all rows and then write data to text file, which is D= 30 and E=25 and B>=152 and B<=243 but it changes B>=153 and B<=244 according to leap years. I tried something but I got 0 and 1. How can I write text file what I want?
load fulltable.txt;
yearlenght = 365+(eomday(fulltable(:,1),2) == 29);
if fulltable(:,1) == yearlenght;
test = fulltable(fulltable(:,2)>=153 & fulltable(:,2)<=244 & fulltable(:,4)==30 & fulltable(:,5)==25,:);
else fulltable(:,1) ~= yearlenght;
test = fulltable(fulltable(:,2)>=152 & fulltable(:,2)<=243 & fulltable(:,4)==30 & fulltable(:,5)==25,:);
end

8 Comments

It's not clear for me what "...but it changes B>=153 and B<=244 according to leap years" means. If possible, could you upload your fulltable.txt file here?
Day of the year of summer season begins with 152 and ends with 243 according to 365 days such as 1998, 1999,2001,2002,2003 and so on.
Day of the year of summer season begins between 153 and ends with 244 according to 366 days which mean leap year such as 2000,2004,2008,2012, 2016, and so on.
I'd like to get data interval between summer season according to years.
%%I renamed fulltable.txt as data.txt
load data.txt
if rem(data(:,1),4) == 0
test = data(data(:,2)>=153 & data(:,2)<=244 & data(:,4)==30 & data(:,5)==25,:);
else
test = data(data(:,2)>=152 & data(:,2)<=243 & data(:,4)==30 & data(:,5)==25,:);
end
It writes data interval between 152 and 243.
Stephen23
Stephen23 on 21 Mar 2019
Edited: Stephen23 on 21 Mar 2019
@Suat YAZICI: your use of IF and ELSE are likely not correct. In particular:
  1. it is very doubtful that you actually know how IF works when its condition is non-scalar (read the IF help carefully), and
  2. you apparently tried to use ELSE with a condition, which is not an error but it does not do anything useful (the trailing "condition" is an actually an unrelated statement).
In any case I suspect that IF is a red-herring and you should just be using basic logical indexing.
I keep getting stuck on conditional because I don't understand how it doesn't work.
"In any case I suspect that IF is a red-herring and you should just be using basic logical indexing."
Can you give me alternative? Should I use find function?
A bit of explanation:
"if" tests on either 0 or positive. Though you should make it either 0 or 1 to make it clear. In your case, 'rem(data(:,1),4)' is a vector, as in there are several numbers. Which of the numbers do you want to test for?
Regarding 'else', I suspect you don't actually need a condition (as you've already modified in another comment.) Though for your information, you can use 'elseif' instead if you want to make another test. So it goes like this:
a=3;
if a==1
fprintf('a=1\n')
elseif a==2
fprintf('a=2\n')
else
fprintf('a is unknown\n')
end
In the example above, if 'a' is actually a vector of [0 1 2 3] then the result would be 'unknown' because the test is only for the first value of 'a'. And this is likely the root of your problem: you're testing for a vector, but 'if' only checks the first value.
By the way, I don't know exactly what you intend, but I would prefer to use datenum/datestr instead of working with months/years etc. directly.
Stephen23
Stephen23 on 22 Mar 2019
Edited: Stephen23 on 22 Mar 2019
'...because the test is only for the first value of 'a'. And this is likely the root of your problem: you're testing for a vector, but 'if' only checks the first value.'
This is incorrect. Nowhere in the MATLAB documention does it mention anything about "first value" being checked by the IF operator. In fact the IF documentation clearly states "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false". Note that it states that all elements must be non-zero for the IF condition to be considered true, not just the first one as Martin Brorsen incorrectly claims.
Here is a simple test which shows that Martin Brorsen's explanation is incorrect, and that the MATLAB documenation correctly describes how MATLAB behaves
>> if [1,0,0], disp('only first'), else disp('wrong!'), end
wrong!
>> if [1,2,3], disp('all elements'), else disp('wrong!'), end
all elements
'"if" tests on either 0 or positive. '
This is incorrect. In fact the MATLAB documentation makes it clear that it tests for nonzero value (which can be positive or negative). This is easy to confirm:
>> if -1, disp('nonzero'), end
nonzero
To know how the IF operator works, read the IF documentation:
Okay, my mistake. But my comment still stands in regards to the above question. Please stay on track when answering questions.
(and there's really no need to repeatedly edit your answer, it's just spamming activity feed)

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 22 Mar 2019
Edited: Stephen23 on 22 Mar 2019
This code sorts the file data into one cell array C, where each cell in C contains the summer data for one year. The summer start and end dates are adjusted for the leap years.
mat = dlmread('fulltable.txt');
ily = (mod(mat(:,1),4)==0&mod(mat(:,1),100))|~mod(mat(:,1),400); % leap year.
idx = mat(:,2)>=(152+ily) & mat(:,2)<=(243+ily); % summer start and end dates.
idr = find(idx);
idg = cumsum([true;diff(idr)~=1]); % years into group numbers.
idz = find(mat(:,4)==30 & mat(:,5)==25); % match D and E column data.
fun = @(r){mat(intersect(r,idz),:)};
C = accumarray(idg,idr,[],fun);
And checking the start and end dates by looking at the first and last few rows of some of the cells:
>> C{1}(1:4,:)
ans =
1998 152 1 30 25 12.5
1998 152 3 30 25 10.9
1998 152 5 30 25 9.6
1998 152 7 30 25 8.4
>> C{1}(end-3:end,:)
ans =
1998 243 17 30 25 34.6
1998 243 19 30 25 32.5
1998 243 21 30 25 29.4
1998 243 23 30 25 20.5
>> C{3}(1:4,:)
ans =
2000 153 1 30 25 29.6
2000 153 3 30 25 25.4
2000 153 5 30 25 20.8
2000 153 7 30 25 15.7
>> C{3}(end-3:end,:)
ans =
2000 244 17 30 25 44.3
2000 244 19 30 25 44.8
2000 244 21 30 25 37.4
2000 244 23 30 25 29.5
You can then simply loop over the cells of the that cell array and export the data in files. Read the documentation to know how to access data in a cell array and save sequential files:

More Answers (1)

Thank you to everyone for explanation. I did it finally.
load data.txt;
[row col] = size(data);
test=[];
format short
fileID=fopen('result.txt','w');
for i=1:row
if mod(data(i,1),4) == 0 && data(i,2)>=153 && data(i,2)<=244 && data(i,4)==30 && data(i,5)==25
test=[test; data(i,:)];
fprintf(fileID,'%d %d %d %2.1f %2.1f %3.1f \n',data(i,:));
else if mod(data(i,1),4) ~= 0 && data(i,2)>=152 && data(i,2)<=243 && data(i,4)==30 && data(i,5)==25
test=[test; data(i,:)];
fprintf(fileID,'%d %d %d %2.1f %2.1f %3.1f \n',data(i,:));
end
end
end
fclose(fileID)

1 Comment

Stephen23
Stephen23 on 22 Mar 2019
Edited: Stephen23 on 22 Mar 2019
You can easily avoid the loop using logical indices (see my answer), which gives simpler, much more efficient code (with the same output file):
fmt = '%d %d %d %2.1f %2.1f %3.1f\n';
mat = dlmread('fulltable.txt');
ily = (mod(mat(:,1),4)==0&mod(mat(:,1),100))|~mod(mat(:,1),400);
idx = mat(:,2)>=(152+ily) & mat(:,2)<=(243+ily);
idz = mat(:,4)==30 & mat(:,5)==25;
fid = fopen('fulltable_2.txt','wt');
fprintf(fid,fmt,mat(idx&idz,:).');
fclose(fid);
Compared:
Elapsed time is 3.059008 seconds. % your answer
Elapsed time is 0.044557 seconds. % my code

Sign in to comment.

Asked:

on 19 Mar 2019

Edited:

on 22 Mar 2019

Community Treasure Hunt

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

Start Hunting!