MATLAB: Error with “timetable” when using the same “VariableNames” argument as for successful use of “table”

columndimensionnamesdimensionsheadersMATLABtabletimetablevariablenames

I am not able to use the same 'VariableNames' argument for "timetable" as for the "table" function.
Here is an example. These two commands run fine and create a table and a timetable with columns named "Date" and "Info".
>> t1 = table(dates, info, 'VariableNames', {'Date', 'Info'});
>> tt1 = table2timetable(t1);
But the following command creates an error:
>> tt2 = timetable(dates, info,'VariableNames', {'Date', 'Info'});
Error using matlab.internal.tabular.private.tabularDimension/assignLabels (line 365)
The VariableNames property must contain one name for each variable in the table.
Error in matlab.internal.tabular.private.varNamesDim/validateAndAssignLabels (line 332)
obj = obj.assignLabels(newLabels,fullAssignment,varIndices);
Error in matlab.internal.tabular.private.tabularDimension/setLabels (line 173)
obj = obj.validateAndAssignLabels(newLabels,indices,fullAssignment,...
fixDups,fixEmpties,fixIllegal);
Error in matlab.internal.tabular.private.tabularDimension/createLike_impl (line 355)
obj = obj.setLabels(dimLabels,[]);
Error in matlab.internal.tabular.private.varNamesDim/createLike (line 70)
obj = obj.createLike_impl(dimLength,dimLabels);
Error in tabular/initInternals (line 207)
t.varDim = t.varDim.createLike(nvars,varnames); % error if invalid, ...
duplicate, or empty
Error in timetable (line 233)
t = t.initInternals(vars, numRows, rowtimes, numVars, varnames);
I do not understand the "timetable" failure, since the arguments are identical to the successful "table" constructor call.

Best Answer

This issue has to do with the different way "timetable" treats the column that represents the time. When you specify "VariableNames" for "timetable", it refers only to the columns other than the time column.
So to achieve the desired outcome in the example, you can execute the following command
>> tt2 = timetable(dates, info, 'VariableNames', {'Info'});
If you would like to change the name of the time column as well (from the default "Time" to "Date"), this can be done with the "DimensionNames" property:
>> tt2.Properties.DimensionNames = {'Date', 'Variables'};
This information can be found in the documentation: