Timetables are a type of tables in MATLAB that store a timestamp corresponding to each row. Similar to tables, timetables can store column-oriented data under a variable name (column name), where every variable has same number of rows. All the functions of a table work with timetables as well along with some timetable-specific functions that are used to align, combine and perform various calculations with other timetables. Let us see various ways of creating timetables.
Method 1:Using timetable()
A timetable can be created with the timetable function. This function takes a time vector and different vector variables.
Syntax:
timetable (<time vector>, var1, ..., var n>)
Here, all var1...var n must have the same number if rows. See the following example for better understanding.
Example 1:
Matlab
% MATLAB code for timetable()
% time vector
tm_vector = datetime({'2022-10-13 23:00:00';
'2022-10-17 19:00:00';'2022-11-03 11:00:00' });
names = ["Harry";"Mark";"Dean"];
job = ["IT";"Management";"HR"];
% Creating timetable
timetable(tm_vector,names,job)
Output:
This will create a table of 3x2 and not 3x3 because the time stamp is not considered a column vector.
Method 2: Using table2timetable()
Converting a table into timetable by the table2timetable function. This function creates a timetable from a function that already has a time vector as one of its column vectors. See the following example to understand its working.
Example 2:
Matlab
% MATLAB code for table2timetable()
tm_vector = datetime({'2022-10-13 23:00:00';
'2022-10-17 19:00:00';'2022-11-03 11:00:00' });
names = ["Harry";"Mark";"Dean"];
job = ["IT";"Management";"HR"];
% Creating table
t=table(tm_vector,names,job);
% Converting to timetable
tt=table2timetable(t);
Output:
Method 3: Using istimetable()
The istimetable() function returns a Boolean value. True if the given table is a timetable else false. See the following example
Example 3:
Matlab
% MATLAB code for istimetable()
tm_vector = datetime({'2022-10-13 23:00:00';
'2022-10-17 19:00:00';'2022-11-03 11:00:00' });
names = ["Harry";"Mark";"Dean"];
job = ["IT";"Management";"HR"];
% Creating table
t=timetable(tm_vector,names,job);
istimetable(t)
Output:
Accessing Elements:
You can access the elements of a timetable in the same manner as ordinary tables by any of the following methods:
- Smooth parenthesis
- Dot notation
- Curly braces notation
Refer to this GfG article for further explanation of accessing tables in MATLAB.
Conclusion:
This article discussed various methods of creating timetables in MATLAB and also briefly explained how to access the elements of a table in MATLAB.
Similar Reads
Simulink in MATLAB MATLAB which is also known as "Matrix Laboratory" allows its user to manipulate matrixes, plotting of functions and data, creation of algorithms, and user interfaces written in different programming languages by providing a computing environment. MATLAB is software designed and powered by mathworks.
4 min read
Dates and Time in MATLAB MATLAB provides many ways to deal with date and time in form of DateTime, calendar duration, and duration data types. These data types do not only support storing and representing date-times but, also allow operations on date time. We shall look at these three data types separately. DateTimeThe date
2 min read
MATLAB - Data Types MATLAB is a platform which provides millions of Engineers and Scientists to analyze data using programming and numerical computing algorithm and also help in creating models. Data types are particular types of data items defined by the values they can store in them, generally, in programming languag
5 min read
Text Formatting in MATLAB In the real-world data can be any form. This data may be in binary, numeric, text, array, and so on. And this data can be converted into text. In Matlab, the text can be formatted using a formatting operator along with formatting functions such as sprintf, numstr, fprintf, compose. These functions/o
4 min read
Trigonometric Functions in MATLAB In this article, we are going to discuss trigonometric functions and their types in MATLAB. Trigonometric functions are the mathematical functions that can result in the output with the given input. There are six trigonometric functions - Sine (sin)Cosine(cos)Tangent(tan)CoTangent(cot)Secant(sec)Co
5 min read
MATLAB - Environment Setup In this article, we are going to discuss setting up a MATLAB Environment from scratch, i.e., from installation to understanding the environment. But before going to that, let's first understand what exactly is MATLAB and its environment. MATLAB is a programming language, which is specially designed
4 min read