How to reshape data for plot?

5 views (last 30 days)
hit8hot
hit8hot on 27 Jan 2016
Commented: dpb on 27 Jan 2016
Hello everyone, I am working with one year data(attached herewith) of latitudes (1st column) and electron contents (2nd column). File contains date at the top, number of data point (eg. 51) for each day then two columns data. This sequence is repeated for all days of the year 2008. I want days of the year (DOY) in one column and maximum value of electron content of each day in other column from the data file for the plot. Your help will be appreciated.

Accepted Answer

dpb
dpb on 27 Jan 2016
Edited: dpb on 27 Jan 2016
That there are a differing number of entries per day makes it a little more of a challenge; that they kindly gave you the count instead of having to find out that number is a boon...
fid=fopen('lat_econtent.txt','rt'); % open file
da=fscanf(fid,'%d',3); % get first date
nDay=365+isleapyr(d(1)); % how many days in this year
eMax=zeros(nDay,2); % allocate space for that many
frewind(fid) % reposition to get clean start to loop
for i=1:nDay
da=fscanf(fid,'%d',3); % get first date
n=fscanf(fid,'%d',1); % get number observations for day
dat=cell2mat(textscan(fid,'',n)); % scan that many
[~,ix]=max(dat(:,2)); % find max location
eMax(i,:)=dat(ix,:); % get lat and max for first
end
fid=fclose(fid);
The result of the above here produced via
>> emax
>> subplot(2,1,1)
>> plot(1:length(eMax),eMax(:,2))
>> subplot(2,1,2)
>> plot(1:length(eMax),eMax(:,1))
NB: I included the location as well as the max; figured it would be the obvious next step despite your not having asked for it... :)
ADDENDUM
You don't need any additional variable for DOY; it's the same as the sequential storage location, 1:nDay.
Oh, and isleapyr is a one-liner utility routine --
function is=isleapyr(yr)
% returns T for input year being a leapyear
is=eomday(yr,2)==29;
  2 Comments
hit8hot
hit8hot on 27 Jan 2016
Thank you. Your reply is greatly appreciated.
dpb
dpb on 27 Jan 2016
No problem; glad to help. I've no klew what "electron content" really is, but it's an interesting pattern over the year.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!