| Contents | Index |
| On this page… |
|---|
Create .NET Object From Constructor |
This example shows how to access functionality already loaded on your system. The topics following the example introduce some key steps and ideas to help you get started using .NET in MATLAB.
The Microsoft .NET Framework class library contains classes, such as System.DateTime, you can use in MATLAB. The following code creates an object and uses DateTime properties and methods to display information about the current date and time.
%Create object for current date and time dateObj = System.DateTime.Now; %Display properties dateObj.DayOfWeek dateObj.Hour %Call methods ToShortTimeString(dateObj) AddDays(dateObj,7); %Call static method System.DateTime.DaysInMonth(dateObj.Year,dateObj.Month)
The following topics provide more information about creating and viewing information about objects and an introduction to .NET data types.
For information about the .NET Framework class library, refer to the 3rd party documentation described in To Learn More About the .NET Framework.
The example in the previous section uses the Now property to create a DateTime object. The following example shows how to create an object using one of the DateTime constructors.
myDate = System.DateTime(2000,1,31);
To call this constructor, or any method, you need to know its argument list, or function signature. Your vendor product documentation shows the function signatures. You can also display the signatures using the MATLAB methodsview function. Type methodsview('System.DateTime') and search the list for DateTime entries, such as shown in the following table.
| Return Type | Name | Arguments |
|---|---|---|
| System.DateTime obj | DateTime | (int32 scalar year, etc. |
From the .NET Class Framework documentation, the following signature initializes a new instance of the DateTime structure to the specified year, month, and day, which is the information required for the myDate variable.
| Return Type | Name | Arguments |
|---|---|---|
| System.DateTime obj | DateTime | (int32 scalar year, int32 scalar month, int32 scalar day) |
For more information, see Reading Method Signatures.
Although the vendor documentation contains information about DateTime objects, you can use MATLAB commands, like properties and methods, to display information about .NET objects. For example:
%Display an object dateObj = System.DateTime.Now %Display its properties properties System.DateTime %Display its methods methods System.DateTime
MATLAB displays the following information. (The property values reflect your specific date and time.)
Display of DateTime Properties
For more information, see:
To use .NET objects in MATLAB, you need to understand how MATLAB treats .NET data types. For example, the following DateTime properties and methods create variables of various .NET types:
dateObj = System.DateTime.Now; thisDay = dateObj.DayOfWeek; thisHour = dateObj.Hour; thisDate = ToLongDateString(dateObj); thisTime = ToShortTimeString(dateObj); monthSize = System.DateTime.DaysInMonth(dateObj.Year,dateObj.Month); whos
Name Size Bytes Class dateObj 1x1 112 System.DateTime monthSize 1x1 4 int32 thisDate 1x1 112 System.String thisDay 1x1 104 System.DayOfWeek thisHour 1x1 4 int32 thisTime 1x1 112 System.String
MATLAB displays the type as a class name.
To use these variables in MATLAB, consider the following:
Numeric values (int32) — MATLAB preserves .NET numeric types by mapping them into equivalent MATLAB types. In the following example, h is type int32.
h = thisHour + 1;
For more information, see .NET Type to MATLAB Type Mapping and Numeric Classes.
Strings (System.String) — Use the char function to convert a System.String object to a MATLAB string:
disp(['The time is ' char(thisTime)]);
Objects (System.DateTime) — Refer to the .NET Framework class library documentation for information about using a DateTime object.
Enumerations (System.DayOfWeek) — According to the DateTime documentation, DayOfWeek is an enumeration. To display the enumeration members, type:
enumeration('thisDay')For more information, see .NET Enumerations in MATLAB.
For a complete list of supported types and mappings, see Handling .NET Data in MATLAB.
![]() | Examples Using .NET | Load a Global .NET Assembly | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |