How can I get and write data from text file?
Show older comments
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
Akira Agata
on 20 Mar 2019
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?
Suat YAZICI
on 20 Mar 2019
Edited: Suat YAZICI
on 20 Mar 2019
Suat YAZICI
on 21 Mar 2019
Edited: Suat YAZICI
on 21 Mar 2019
@Suat YAZICI: your use of IF and ELSE are likely not correct. In particular:
- it is very doubtful that you actually know how IF works when its condition is non-scalar (read the IF help carefully), and
- 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.
Suat YAZICI
on 21 Mar 2019
Martin Brorsen
on 22 Mar 2019
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.
'...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:
Martin Brorsen
on 22 Mar 2019
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)
Accepted Answer
More Answers (1)
Suat YAZICI
on 22 Mar 2019
1 Comment
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
Categories
Find more on Logical 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!