How can I use a while loop to fill in certain cells of an array?
1 view (last 30 days)
Show older comments
I am currently working with rain gauge data read in from excel. Column 1 has rainfall totals from when the gauge tipped over, column 2 has the year, in this case 2017, column 3 has the month starting with January and ending in October, column 4 has each day of the month when the rain gauge tipped. The 3rd and 4th columns vary with how many times each value is shown, ie) day 1 in January shows in three rows and day 2 shows in 10 rows indicating the number of times the gauge tipped in that day. I need the code to find the last tip of each day, take the rainfall amount for that day and place it in the correct cell of a 365 x 1 array, and do it everyday until the end of the data set.
My idea was to use a while loop since I don't know how many times each value re-occurs.
months = Excel_data(:,3);
days = Excel_data(:,4);
n = 1;
M = end;
d = end;
while months(M) = n;
while days(d) = n;
.....
n = n+1;
end
n = n+1;
end
Any insight would be helpful, Thank you.
0 Comments
Answers (1)
Guillaume
on 22 May 2018
Edited: Guillaume
on 22 May 2018
Your data is ideally suited for a table or even a timetable. In fact, if you use a timetable, obtaining what you want is trivial:
newtimetable = retime(yourtimetable, 'daily', 'lastvalue');
Otherwise, assuming your cell array is ordered by day (or at least all values for the same day are grouped together), what you want can be achieved a lot faster than using a loop:
days = Excel_data(:,4);
islastofday = [diff(days) ~= 0; true];
dailydata = Excel_data(islastofday, :);
See Also
Categories
Find more on Spreadsheets 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!