Add zero rows to a matrix with missing days

1 view (last 30 days)
Anton Shagin
Anton Shagin on 27 Nov 2017
Answered: MHZ on 27 Nov 2017
Hi! I went through all questions here but could not find a solution. I have a matrix 6519x20, first 5 columns are Month, Day, Year, Hour and Min. Next 25 are data. Each day has up to 227 values, some days have less data some days have more. There are days that are missing so it would look like an example on a picture where day 17 is missing.I need to determine missing days and insert missing zero rows into the matrix. I only need to insert one row per missing day as I will rearrange the data and swap matrix rows and columns later on. Therefore each day will have the same amount of data points.In the end I should receive a matrix of 6519+N of missing daysx20. Thanks!
  4 Comments
KL
KL on 27 Nov 2017
I would suggest to use timetable for such data. Import your data as table and convert it into a timetable (just one column as datetime instead of 5). To fill out the missing data, there's a dedicated documentation,
Anton Shagin
Anton Shagin on 27 Nov 2017
Thanks, KL. I believe I need to mention that I'm using 2011 version and readtable function was added in 2014.

Sign in to comment.

Answers (1)

MHZ
MHZ on 27 Nov 2017
1. Import the excel sheet into Matlab using xlsread and prepare it as a matrix 2. You will need to convert the dates to their math equivalent in matlab using datnum. 3. Save the first row as is to a new matrix 4.Run a for loop by rows that will check the datenum with the one next to it. If the difference is 0 or 1, then write the next row to the new matrix. else add the missing number(s) with rows of zeros. The amount of the difference will tell you how many rows to add. Example, if the difference is 2, then add 1 row, if 3 then 2 rows,....basically n-1.
I hope this helps.

Community Treasure Hunt

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

Start Hunting!