Contenu principal

Access Simple .NET Classes

R2026b

The topics following these 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.

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.

Create .NET Object from Constructor

The example in the previous section uses the Now property to create a DateTime object. This 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 table for DateTime entries, such as this:

NameReturn TypeArguments
DateTimeSystem.DateTime obj

(int32 scalar year,...)

From the .NET documentation, this 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 Use .NET Methods in MATLAB.

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 this information. (The property values reflect your specific date and time.)

netDate = 
  System.DateTime
  Package: System

  Properties:
           Date: [1x1 System.DateTime]
            Day: 11
      DayOfWeek: [1x1 System.DayOfWeek]
      DayOfYear: 11
           Hour: 12
           Kind: [1x1 System.DateTimeKind]
    Millisecond: 413
         Minute: 31
          Month: 1
            Now: [1x1 System.DateTime]
         UtcNow: [1x1 System.DateTime]
         Second: 38
          Ticks: 634303458984133595
      TimeOfDay: [1x1 System.TimeSpan]
          Today: [1x1 System.DateTime]
           Year: 2011
       MinValue: [1x1 System.DateTime]
       MaxValue: [1x1 System.DateTime]
  Methods, Superclasses
Properties for class System.DateTime:

    Date
    Day
    DayOfWeek
    DayOfYear
    Hour
    Kind
    Millisecond
    Minute
    Month
    Now
    UtcNow
    Second
    Ticks
    TimeOfDay
    Today
    Year
    MinValue
    MaxValue
Methods for class System.DateTime:

Add                    GetType                ToUniversalTime        
AddDays                GetTypeCode            addlistener            
AddHours               IsDaylightSavingTime   delete                 
AddMilliseconds        Subtract               eq                     
AddMinutes             ToBinary               findobj                
AddMonths              ToFileTime             findprop               
AddSeconds             ToFileTimeUtc          ge                     
AddTicks               ToLocalTime            gt                     
AddYears               ToLongDateString       isvalid                
CompareTo              ToLongTimeString       le                     
DateTime               ToOADate               lt                     
Equals                 ToShortDateString      ne                     
GetDateTimeFormats     ToShortTimeString      notify                 
GetHashCode            ToString               

Static methods:

Compare                Parse                  op_GreaterThan         
DaysInMonth            ParseExact             op_GreaterThanOrEqual  
FromBinary             SpecifyKind            op_Inequality          
FromFileTime           TryParse               op_LessThan            
FromFileTimeUtc        TryParseExact          op_LessThanOrEqual     
FromOADate             op_Addition            op_Subtraction         
IsLeapYear             op_Equality 

For more information, see:

Use .NET Data Types in MATLAB

To use .NET objects in MATLAB, you need to understand how MATLAB treats .NET data types. For example, these 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 .NET variables in MATLAB, consider the following:

  • Numeric values (int32) — MATLAB preserves .NET numeric types by mapping them into equivalent MATLAB types. In this example, h is type int32.

    h = thisHour + 1;
    

    For more information, see Return .NET Scalar Data to MATLAB 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 (for example, System.DateTime) — Refer to the .NET class library documentation for information about using a DateTime object.

  • Enumerations (for example, 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

Topics