Access a Simple .NET Class

System.DateTime Example

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
netDate = System.DateTime.Now;

% Display properties
netDate.DayOfWeek
netDate.Hour

% Call methods
ToShortTimeString(netDate)
AddDays(netDate,7);

% Call static method
System.DateTime.DaysInMonth(netDate.Year,netDate.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 third-party documentation described in To Learn More About the .NET Framework.

Create .NET Object from Constructor

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 TypeNameArguments
System.DateTime objDateTime

(int32 scalar year,...)

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 TypeNameArguments
System.DateTime objDateTime(int32 scalar year,
int32 scalar month,
int32 scalar day)

For more information, see Reading Method Signatures.

View Information About .NET Object

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
netDate = 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 Object

 Display of DateTime Properties

 Display of DateTime Methods

For more information, see:

Introduction to .NET Data Types

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:

netDate = System.DateTime.Now;
thisDay = netDate.DayOfWeek;
thisHour = netDate.Hour;
thisDate = ToLongDateString(netDate);
thisTime = ToShortTimeString(netDate);
monthSz = System.DateTime.DaysInMonth(netDate.Year,netDate.Month);
whos
Name           Size  Bytes  Class

netDate        1x1     112  System.DateTime
monthSz        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 Types.

  • Strings (System.String) — Use the char function to convert a System.String object to a MATLAB char array:

    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 Handle Data Returned from .NET Objects.

Related Topics