Variable Names and classes for Tables as well as using the Hour function on datetime variables?

I have a table, B, which is 360x11 and the first column is a date time variable. I want to verify the variable names using:
B.Properties.VarNames(:)
but this command wont work. I get the following.
>> B.Properties.VarNames(:) Unknown table property: VarNames.
That's the first question.
The second is that I want to create new columns in the table, B, which are the Year, Month, Day and Hour of the first column date (B(:,1) is the date)
I am trying to do:
B(:,12)=hour(B(:,1));
to make a new column, number 12, which is equal to the hour of the date in column 1. However, that won't work. Why not?

Answers (2)

For your first question, aren't you supposed to be using,
B.Properties.VariableNames
For the next question, what's the datatype of first column? if it's datetime
B.Hour = B.YourColumnName.Hour;
Or any other name instead of HOUR, the new columns by default added to the left, which theoretically is your 12th column.

4 Comments

Thanks. I'll try that.
I got the Varnames from the Matlab documentation on "Add and Delete Variables". In that doc they use Table.Properties.VarNames(:)
Delete variables by variable number. Delete the variable wgt, the fourth variable in the dataset array.
ds(:,4) = []; ds.Properties.VarNames(:)
The first column is a datetime variable. Why doesn't the syntax I used at first work here? I had thought it would from the documentation on the hour() function?
I got the Varnames from the Matlab documentation
Which version/language of matlab? I can't see that in my version, so it probably got corrected in newer versions. (As an aside there is a property called VarNames but it's private, so not visible or accessible to users of tables).
The same documentation also shows how to add variables.
Version 2017a/English
Maybe the difference is the documentation I was looking at is called "Add and Delete Variables" where as the one you provided the link to is called "Add and Delete Table Variables". Just a random thought. Thanks again for the help.
Was it this documentation page? That page deals with dataset arrays which are part of Statistics and Machine Learning Toolbox. The newer table array included in MATLAB is similar to the older dataset arrays, but there are differences between the two. That property name is one of those differences. We recommend you use table for new code.
Since you're working with table arrays use the documentation for table included in MATLAB rather than the documentation for dataset in Statistics and Machine Learning Toolbox.

Sign in to comment.

KL answered your first question, the property of the table T that lists the variable names is T.Properties.VariableNames.
As for the second:
% Create a table with datetime data
B = table(datetime('today')+days((0:5).'), 'VariableNames', {'Date'})
% Add a new variable named Day
B.Day = day(B.Date)
% Add a new variable named Month
B{:, width(B)+1} = month(B{:, 1})
B.Properties.VariableNames{end} = 'Month'
Note that with the third section of code in my example, the new variable was first created with a default name and I had to update the variable name as a separate step. I left the semicolon off that line so you could see the intermediary step. Compare that with the second section of code, where the new variable had the name Day as soon as it was created.
There are differences in indexing into a table using parentheses, curly braces, and period. This page in the documentation describes these different techniques

4 Comments

Thank you very much for that answer. It really helps. If I also want to display the class of all the variables in a table what function is that? Can I call a function that returns variable names and classes?
Would the following be valid? Where "Date" is the datetime variable.
B.Year=year(B.Date); B.Month=month(B.Date); B.Day=day(B.Date); B.Hour=hour(B.Date);
Would the following be valid?
Yes. But the easiest way for you to know is to try.
I also want to display the class of all the variables in a table what function is that?
There isn't a function for that. But it's easily done:
%replace yourtable by the name of your table
varclass = cellfun(@(v) class(yourtable.(v)), yourtable.Properties.VariableNames, 'UniformOutput', false)
T4H14, regarding your question about displaying classes, you could ask for the class on each variable either one at a time or using varfun. But if you're looking for something that operates on the whole table at once, the summary function seems closest to what you're looking for.

Sign in to comment.

Categories

Asked:

on 24 Oct 2017

Commented:

on 24 Oct 2017

Community Treasure Hunt

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

Start Hunting!