| Contents | Index |
| On this page… |
|---|
Building a .NET Application for MATLAB Examples |
You often need to create objects when working with .NET classes. An object is an instance of a particular class. Methods are functions that operate exclusively on objects of a class. Data types package together objects and methods so that the methods operate on objects of their own type. For more information about objects, see MATLAB Objects.
You construct .NET objects in the MATLAB workspace by calling the class constructor, which has the same name as the class. The syntax to create a .NET object classObj is:
classObj = namespace.ClassName(varargin)
where varargin is the list of constructor arguments to create an instance of the class specified by ClassName in the given namespace. For an example, see Create .NET Object From Constructor.
You can use C# code examples in MATLAB, such as the NetDocCell assembly provided in Converting .NET Arrays to Cell Arrays. Build an application using a C# development tool, like Microsoft Visual Studio and then load it into MATLAB using the NET.addAssembly function. The following are basic steps to do this; consult your development tool documentation for specific instructions.
From your development tool, open a new project and create a C# class library.
Copy the classes and other constructs from the C# files into your project.
Build the project as a DLL.
The name of this assembly is the namespace. Note the full path to the DLL file. Since it is a private assembly, you must use the full path to load it in MATLAB.
After you load the assembly, if you modify and rebuild it, you must restart MATLAB to access the new assembly. You cannot unload an assembly in MATLAB.
The product documentation for your assembly contains information about its classes. However, you can use the NET.addAssembly command to read basic information about an assembly. For example, to view the class names for the private assembly netdoc.NetSample, type:
dllPath = fullfile('c:','work','NetSample.dll'); sampleInfo = NET.addAssembly(dllPath); sampleInfo.Classes
ans =
'netdoc.SampleMethodSignature'
'netdoc.SampleMethods'
If your assembly has hundreds of entries, you can consult the product documentation, or open a window to an online document, such as the System namespace reference page on the Microsoft Developer Network. For information about using this documentation, see To Learn More About the .NET Framework. For example, to find the number of classes nclasses in mscorlib, type:
asm = NET.addAssembly('mscorlib');
[nclasses,x] = size(asm.Classes);Objects created from .NET classes appear in MATLAB as reference types, or handle objects. Calling the delete function on a .NET handle releases all references to that .NET object from MATLAB, but does not invoke any .NET finalizers. The .NET Framework manages garbage collection.
For more information about managing handle objects, see Destroying Objects.
![]() | Getting Started with .NET | Using .NET Properties | ![]() |

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 |