How to add a column of today's date to a table?

10 views (last 30 days)
Hi,
I have a table vTbl with several columns. I would like to add one more column with dates in it. And all the cells in that new column is just today/s date in the format of "mm/dd/yyyy". But I just can't get it right. I tried:
todayDate = datetime('now','Format','MM/dd/yyyy');
vdates = cell(size(vTbl, 1), 1);
todayeDateStr = datestr(todayDate, 'MM/dd/yyyy');
vdates(:) = (todayDateStr);
vTbl = [ table(vdates', 'VariableNames', {'VDate'}) vTbl];
But I keep getting error:
The following error occurred converting from char to cell:
Error using cell
Conversion to cell from char is not possible.
I am stuck here. Thanks for any help.
Jennifer

Accepted Answer

Star Strider
Star Strider on 14 Aug 2015
I had to create ‘vTbl’, but this code should work regardless of what it is:
strs = char(randi([double('A') double('Z')], 5, 3)); % Create Variable
vTbl = table(strs, 'VariableNames',{'Strings'}); % Create ‘vTbl’
todayDate = datestr(now,'mm/dd/yyyy'); % Create Date String
datesvct = repmat(todayDate, size(vTbl,1), 1); % Create ‘Vector’ Of Date String
vTbl = [ table(datesvct, 'VariableNames', {'VDate'}) vTbl]; % Concatenate Dates In Table
  2 Comments
JFz
JFz on 14 Aug 2015
Thank you, Star! It works!
Then what did I do wrong that it didn't work? The only difference is I used cell array but then it should work.
Thank you so much.
Star Strider
Star Strider on 14 Aug 2015
My pleasure!
The table function apparently needs character arrays, not cells. Cell arrays are extremely useful ways of storing data, but you have to take variables out of the cell to calculate with them or use them in most functions.
Again, my pleasure. I learned a bit more about tables, so thank you for asking your Question.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!