Main Content

Access a Simple .NET Class

System.DateTime Example

This example shows how to access functionality already loaded on your system. The System.Windows.Forms Example shows how to load an assembly into MATLAB®.

The topics following the examples introduce some key steps and ideas to help you get started using .NET in MATLAB. For information about assemblies, see Assembly Is a Library of .NET Classes. For information about .NET class libraries, refer to third-party documentation described in To Learn More About .NET.

A Microsoft® .NET assembly has classes, such as System.DateTime, that you can use in MATLAB. This 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)

System.Windows.Forms Example

This example shows you how to make .NET classes visible to MATLAB by loading an assembly using the NET.addAssembly function.

The Windows.Forms class, like any .NET class, is part of an assembly. To work with the class, call NET.addAssembly to load the assembly into MATLAB. Your vendor documentation contains the assembly name.

NET.addAssembly('System.Windows.Forms')
import System.Windows.Forms.*
buttons = MessageBoxButtons.YesNo
result = MessageBox.Show("Yes or no?","Message Box",buttons)

Respond to the prompt to close the message box.

The System.DateTime Example does not call NET.addAssembly because MATLAB dynamically loads its assembly (mscorlib) at startup.

Note

You cannot unload an assembly in MATLAB. If you modify and rebuild your own assembly, you must restart MATLAB to access the changes.

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.

NameReturn TypeArguments
DateTimeSystem.DateTime obj

(int32 scalar year,...)

From the .NET 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.

NameReturn TypeArguments
DateTimeSystem.DateTime obj(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 string function to convert a System.String object to a MATLAB string:

    disp("The time is " + string(thisTime))
  • Objects (System.DateTime) — Refer to .NET 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.

See Also

Related Topics