主要内容

Use .NET Properties in MATLAB

You can access and modify .NET object properties directly from MATLAB®. Public .NET fields and properties appear as MATLAB properties, which you can view, get, and set using dot notation. Properties that require arguments are accessed as methods. Protected and private .NET members are not visible in MATLAB.

View Property Names

To view property names, use the properties function. For example, view the names of the properties of the .NET System.DateTime class.

properties("System.DateTime")
Properties for class System.DateTime:

    Date
    Day
    DayOfWeek
    DayOfYear
    Hour
    Kind
    Millisecond
    Minute
    Month
    Now
    UtcNow
    Second
    Ticks
    TimeOfDay
    Today
    Year
    MinValue
    MaxValue

Get and Set Property Values

To get and set the value of a .NET object property, use MATLAB dot notation:

val = ClassName.PropertyName;
ClassName.PropertyName = val;

For example, create a .NET System.DateTime object and get the value of the Day property, which represents the current day of the month.

dtnow = System.DateTime.Now;
d = dtnow.Day
d =
int32
18

MATLAB represents public .NET fields and properties as MATLAB properties. So, you can set their values using dot notation. For example, set the fields X, Y, and Z of a System.Numerics.Vector3 object.

NET.addAssembly("System.Numerics.Vectors");
v3 = System.Numerics.Vector3;
v3.X = 1;
v3.Y = 0;
v3.Z = -1;
v3.ToString
ans =
<1, 0, -1>

Call .NET Properties That Take an Argument

MATLAB represents a .NET property that takes an argument as a method. For example, the Chars property of the System.String class returns the character at a specified position in a System.String object. MATLAB displays the Chars property as a method.

methods("System.String")
Methods for class System.String:

Chars               Replace             
Clone               Split               
CompareTo           StartsWith          
Contains            String              
CopyTo              Substring           
EndsWith            ToCharArray         
Equals              ToLower             
GetEnumerator       ToLowerInvariant    
GetHashCode         ToString            
GetType             ToUpper             
GetTypeCode         ToUpperInvariant    
IndexOf             Trim                
IndexOfAny          TrimEnd             
Insert              TrimStart           
IsNormalized        char                
LastIndexOf         eq                  
LastIndexOfAny      keyHash             
Normalize           keyMatch            
PadLeft             ne                  
PadRight            string              
Remove              

Static methods:

Compare             IsNullOrEmpty       
CompareOrdinal      IsNullOrWhiteSpace  
Concat              Join                
Copy                ReferenceEquals     
Format              op_Equality         
Intern              op_Inequality       
IsInterned          

The Chars method has this signature.

Return TypeNameArguments
char scalar RetValChars(System.String this,
int32 scalar index)

Display the first character in a string.

str = System.String("My new string");
Chars(str,0)
ans =
'M'

See Also

| |