Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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?

Antworten (2)

KL
KL am 24 Okt. 2017
Bearbeitet: KL am 24 Okt. 2017
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 Kommentare
T4H14
T4H14 am 24 Okt. 2017
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.
Steven Lord
Steven Lord am 24 Okt. 2017
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.

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 24 Okt. 2017
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 Kommentare
Guillaume
Guillaume am 24 Okt. 2017
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)
Steven Lord
Steven Lord am 24 Okt. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by