Adding time and date to table
41 views (last 30 days)
Show older comments
Benedikt Skurk
on 2 Mar 2021
Commented: Benedikt Skurk
on 2 Mar 2021
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
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)
More Answers (0)
See Also
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!