Adding time and date to table

64 views (last 30 days)
Hi i want to read a csv file and add in the first column from row 3 to end a date and time vector which i generate with the following code
ElmLnecloading = readtable('ElmLne_c_loading.csv');
t1 = datetime(2017,1,1,0,15,0);
t2 = datetime(2017,12,31,23,45,0);
interval = minutes(15);
Vector = (t1:interval:t2);
DateString = char(Vector');
disp(DateString)
ElmLnecloading(3:end,1) = DateString;
disp(ElmLnecloading)
but i always get the following error:
To assign to or create a variable in a table, the number of rows must match the height of the table.
A picture of a part of the file is attached. It has 35041 rows
Can someone help me how to solve the problem or having an easier way to solve my problem? I am pretty new to Matlab...

Accepted Answer

Cris LaPierre
Cris LaPierre on 2 Mar 2021
Edited: Cris LaPierre on 2 Mar 2021
In MATLAB, all elements of one table variable should be of the same data type. For this reason, I suspect your table in MATLAB has removed the first 2 rows. This means (3:end,1) is actually 2 rows shorter than DateString, resulting in the size mismatch error.
Tables allow you to have variables of different datatypes in the same table. I would therefore recommend keeping your dates as datetimes.
t1 = datetime(2017,1,1,0,15,0);
t2 = datetime(2017,12,31,23,45,0);
interval = minutes(15);
Vector = (t1:interval:t2)';
data = rand(length(Vector),1);
dTbl = table(Vector,data)
dTbl = 35039x2 table
Vector data ____________________ _______ 01-Jan-2017 00:15:00 0.13599 01-Jan-2017 00:30:00 0.90735 01-Jan-2017 00:45:00 0.9517 01-Jan-2017 01:00:00 0.63316 01-Jan-2017 01:15:00 0.40791 01-Jan-2017 01:30:00 0.31211 01-Jan-2017 01:45:00 0.97797 01-Jan-2017 02:00:00 0.46379 01-Jan-2017 02:15:00 0.28019 01-Jan-2017 02:30:00 0.10032 01-Jan-2017 02:45:00 0.51595 01-Jan-2017 03:00:00 0.64312 01-Jan-2017 03:15:00 0.87067 01-Jan-2017 03:30:00 0.98085 01-Jan-2017 03:45:00 0.61279 01-Jan-2017 04:00:00 0.65733

More Answers (0)

Categories

Find more on Dates and Time 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!