| Contents | Index |
Before you begin integrating your component code with your .NET application, it is helpful to understand how the elements of the Deployment Tool project map to the class names in your generated wrapper code, and the naming conventions used for class and methods names in this code.
The builder project contains the files and settings needed by the MATLAB Builder NE product to create a deployable .NET component. A project specifies information about classes and methods, including the MATLAB functions to be included.
The builder transforms MATLAB functions that are specified in the component's project to methods belonging to a managed class.
When creating a component, you must provide one or more class names as well as a component name. The component name also specifies the name of the assembly that implements the component. The class name denotes the name of the class that encapsulates MATLAB functions.
To access the features and operations provided by the MATLAB functions, instantiate the managed class generated by the builder, and then call the methods that encapsulate the MATLAB functions.
Typically you should specify names for components and classes that will be clear to programmers who use your components. For example, if you are encapsulating many MATLAB functions, it helps to determine a scheme of function categories and to create a separate class for each category. Also, the name of each class should be descriptive of what the class does.
The .NET Framework General Reference recommends the use of Pascal case for capitalizing the names of identifiers of three or more characters. That is, the first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. For example:
MakeSquare
In contrast, MATLAB programmers typically use all lowercase for names of functions. For example:
makesquare
By convention, the MATLAB Builder NE examples use Pascal case.
Valid characters are any alpha or numeric characters, as well as the underscore (_) character.
The builder supports the standard versioning capabilities provided by the .NET Framework.
Note You can make side-by-side invocations of multiple versions of a component within the same application only if they access the same version of the MCR. |
There are many instances when you may need to convert various native data types to types compatible with MATLAB. Use this section as a guideline to performing some of these basic tasks.
See Data Conversion Rules for complete tables detailing type-to-type data conversion rules using MATLAB Builder NE.
Tip Learn about creating type-safe interfaces for .NET components, in order to avoid data conversion tasks with MWArray. See Type-Safe Interfaces, WCF, and MEF for details. |
To support data conversion between managed types and MATLAB types, the builder provides a set of data conversion classes derived from the abstract class, MWArray.
The MWArray data conversion classes allow you to pass most native .NET value types as parameters directly without using explicit data conversion. There is an implicit cast operator for most native numeric and string types that will convert the native type to the appropriate MATLAB array.
When you invoke a method on a component, the input and output parameters are a derived type of MWArray. To pass parameters, you can either instantiate one of the MWArray subclasses explicitly, or, in many cases, pass the parameters as a managed data type and rely on the implicit data conversion feature of .NET Builder.
Overview of Classes and Methods in the Data Conversion Class Hierarchy. To support MATLAB data types, the MATLAB Builder NE product provides the MWArray data conversion classes in the MATLAB Builder NE MWArray assembly. You reference this assembly in your managed application to convert native arrays to MATLAB arrays and vice versa.
See the MWArray API documentation on the MATLAB Builder NE Documentation Roadmap page (on the Web on in the product help) for full details on the classes and methods provided.
The data conversion classes are built as a class hierarchy that represents the major MATLAB array types.
Note See Overview for an introduction to the classes and see MWArray Class Library Reference (available online only) for details about this class library. |
The root of the hierarchy is the MWArray abstract class. The MWArray class has the following subclasses representing the major MATLAB types: MWNumericArray, MWLogicalArray, MWCharArray, MWCellArray, and MWStructArray.
MWArray and its derived classes provide the following functionality:
Constructors and destructors to instantiate and dispose of MATLAB arrays
Properties to get and set the array data
Indexers to support a subset of MATLAB array indexing
Implicit and explicit data conversion operators
General methods
Using Cell and Struct Arrays with MWArray. You must use .NET Remoting to integrate .NET cell and struct arrays with MWArray.
See The Native .NET Cell and Struct Example for more information and a complete end-to-end example.
Note Because the conversion process is automatic (in most cases), you do not need to understand the conversion process to pass and return arguments with MATLAB Builder NE components. |
In most instances, if a native .NET primitive or array is used as an input parameter in a C# program, the builder transparently converts it to an instance of the appropriate MWArray class before it is passed on to the component method. The builder can convert most CLS-compliant string, numeric type, or multidimensional array of these types to an appropriate MWArray type.
Note This conversion is transparent in C# applications, but might require an explicit casting operator in other languages, for example, op_implicit in Visual Basic®. |
Here is an example. Consider the .NET statement:
result = theFourier.plotfft(3, data, interval);
In this statement the third argument, namely interval, is of the .NET native type System.Double. The builder casts this argument to a MATLAB 1-by-1 double MWNumericArray type (which is a wrapper class containing a MATLAB double array).
See Data Conversion Rules for a list of all the data types that are supported along with their equivalent types in the MATLAB product.
Note There are some data types commonly used in the MATLAB product that are not available as native .NET types. Examples are cell arrays, structure arrays, and arrays of complex numbers. Represent these array types as instances of MWCellArray, MWStructArray, and MWNumericArray, respectively. |
Multidimensional Array Processing in MATLAB and .NET. MATLAB and .NET implement different indexing strategies for multidimensional arrays. When you create a variable of type MWNumericArray, MATLAB automatically creates an equivalent array, using its own internal indexing. For example, MATLAB indexes using this schema:
(row column page1 page2 ...)
while .NET indexes as follows:
(... page2 page1 row column)
Given the multi-dimensional MATLAB myarr:
>> myarr(:,:,1) = [1, 2, 3; 4, 5, 6];
>> myarr(:,:,2) = [7, 8, 9; 10, 11, 12];
>> myarr
myarr(:,:,1) =
1 2 3
4 5 6
myarr(:,:,2) =
7 8 9
10 11 12
You would code this equivalent in .NET:
double[,,] myarr = {{{1.000000, 2.000000, 3.000000},
{4.000000, 5.000000, 6.000000}}, {{7.000000, 8.000000,
9.000000}, {10.000000, 11.000000, 12.000000}}};
Native Data Conversion. The builder provides MATLAB array classes in order to facilitate data conversion between native data and compiled MATLAB functions.
This example explicitly creates a numeric constant using the constructor for the MWNumericArray class with a System.Int32 argument. This variable can then be passed to one of the generated MATLAB Builder NE methods.
int data = 24;
MWNumericArray array = new MWNumericArray(data);
Console.WriteLine("Array is of type " + array.NumericType);When you run this example, the results are:
Array is of type double
In this example, the native integer (int data) is converted to an MWNumericArray containing a 1-by-1 MATLAB double array, which is the default MATLAB type.
Tip To preserve the integer type (rather than convert to the default double type), you can use the constructor provided by MWNumericArray for this purpose. Preserving the integer type can help to save space. |
The MATLAB Builder NE product does not support some MATLAB array types because they are not CLS-compliant. See Unsupported MATLAB Array Types for a list of the unsupported types.
For more information about the concepts involved in data conversion, see Managing Data Conversion Issues with MATLAB Builder NE Data Conversion Classes.
Type Specification. If you want to create a MATLAB numeric array of a specific type, set the optional makeDouble argument to False. The native type then determines the type of the MATLAB array that is created.
Here, the code specifies that the array should be constructed as a MATLAB 1-by-1 16-bit integer array:
short data = 24;
MWNumericArray array = new MWNumericArray(data, false);
Console.WriteLine("Array is of type " + array.NumericType);
Running this example produces the following results:
Array is of type int16
Optional Argument Specification. In the MATLAB product, varargin and varargout are used to specify arguments that are not required. Consider the following MATLAB function:
function y = mysum(varargin)
y = sum([varargin{:}]);
This function returns the sum of the inputs. The inputs are provided as a varargin, which means that the caller can specify any number of inputs to the function. The result is returned as a scalar double array.
For the mysum function, the MATLAB Builder NE product generates the following interfaces:
// Single output interfaces
public MWArray mysum()
public MWArray mysum(params MWArray[] varargin)
// Standard interface
public MWArray[] mysum(int numArgsOut)
public MWArray[] mysum(int numArgsOut,
params MWArray[] varargin)
// feval interface
public void mysum(int numArgsOut, ref MWArray ArgsOut,
params MWArray[] varargin)
The varargin arguments can be passed as either an MWArray[], or as a list of explicit input arguments. (In C#, the params modifier for a method argument specifies that a method accepts any number of parameters of the specific type.) Using params allows your code to add any number of optional inputs to the encapsulated MATLAB function.
Here is an example of how you might use the single output interface of the mysum method in a .NET application:
static void Main(string[] args]
{
MWArray sum= null;
MySumClass mySumClass = null;
try
{
mySumClass= new MySumClass();
sum= mySumClass.mysum((double)2, 4);
Console.WriteLine("Sum= {0}", sum);
sum= mySumClass.mysum((double)2, 4, 6, 8);
Console.WriteLine("Sum= {0}", sum);
}
}
The number of input arguments can vary.
Note For this particular signature, you must explicitly cast the first argument to MWArray or a type other than integer. Doing this distinguishes the signature from the method signature, which takes an integer as the first argument. If the first argument is not explicitly cast to MWArray or as a type other than integer, the argument can be mistaken as representing the number of output arguments. |
Construct a Single Input Argument
Pass a Variable Number of Outputs. When present, varargout arguments are handled in the same way that varargin arguments are handled. Consider the following MATLAB function:
function varargout = randvectors()
for i=1:nargout
varargout{i} = rand(1, i);
end
This function returns a list of random double vectors such that the length of the ith vector is equal to i. The builder generates a .NET interface to this function as follows:
public void randvectors() public MWArray[] randvectors(int numArgsOut) public void randvectors(int numArgsOut, ref MWArray[] varargout)
The previous examples show guidelines to use if you know the type and dimensionality of the output argument. Sometimes, in MATLAB programming, this information is unknown, or can vary. In this case, the code that calls the method might need to query the type and dimensionality of the output arguments.
There are two ways to make the query:
Use .NET reflection to query any object for its type.
Use any of several methods provided by the MWArray class to query information about the underlying MATLAB array.
.NET Reflection. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. See the MSDN Library for more information about reflection.
The following code sample calls the myprimes method, and then determines the type using reflection. The example assumes that the output is returned as a numeric vector array but the exact numeric type is unknown.
public void GetPrimes(int n)
{
MWArray primes= null;
MyPrimesClass myPrimesClass= null;
try
{
myPrimesClass= new MyPrimesClass();
primes= myPrimesClass.myprimes((double)n);
Array primesArray= ((MWNumericArray)primes).
ToVector(MWArrayComponent.Real);
if (primesArray is double[])
{
double[] doubleArray= (double[])primesArray;
/* Do something with doubleArray . . . */
}
else if (primesArray is float[])
{
float[] floatArray= (float[])primesArray;
/* Do something with floatArray . . . */
}
else if (primesArray is int[])
{
int[] intArray= (int[])primesArray;
/*Do something with intArray . . . */
}
else if (primesArray is long[])
{
long[] longArray= (long[])primesArray;
/*Do something with longArray . . . */
}
else if (primesArray is short[])
{
short[] shortArray= (short[])primesArray;
/*Do something with shortArray . . . */
}
else if (primesArray is byte[])
{
byte[] byteArray= (byte[])primesArray;
/*Do something with byteArray . . . */
}
else
{
throw new ApplicationException("
Bad type returned from myprimes");
}
}
}The example uses the toVector method to return a .NET primitive array (primesArray), which represents the underlying MATLAB array. See the following code fragment from the example:
primes= myPrimesClass.myprimes((double)n); Array primesArray= ((MWNumericArray)primes). ToVector(MWArrayComponent.Real);
Note The toVector is a method of the MWNumericArray class. It returns a copy of the array component in column major order. The type of the array elements is determined by the data type of the numeric array. |
MWArray Query. The next example uses the MWNumericArray NumericType method, along with MWNumericType enumeration to determine the type of the underlying MATLAB array. See the switch (numericType) statement.
public void GetPrimes(int n)
{
MWArray primes= null;
MyPrimesClass myPrimesClass= null;
try
{
myPrimesClass= new MyPrimesClass();
primes= myPrimesClass.myprimes((double)n);
if ((!primes.IsNumericArray) || (2 !=
primes.NumberofDimensions))
{
throw new ApplicationException("Bad type returned
by mwprimes");
}
MWNumericArray _primes= (MWNumericArray)primes;
MWNumericType numericType= _primes.NumericType;
Array primesArray= _primes.ToVector(
MWArrayComponent.Real);
switch (numericType)
{
case MWNumericType.Double:
{
double[] doubleArray= (double[])primesArray;
/* (Do something with doubleArray . . .) */
break;
}
case MWNumericType.Single:
{
float[] floatArray= (float[])primesArray;
/* (Do something with floatArray . . .) */
break;
}
case MWNumericType.Int32:
{
int[] intArray= (int[])primesArray;
/* (Do something with intArray . . .) */
break;
}
case MWNumericType.Int64:
{
long[] longArray= (long[])primesArray;
/* (Do something with longArray . . .) */
break;
}
case MWNumericType.Int16:
{
short[] shortArray= (short[])primesArray;
/* (Do something with shortArray . . .) */
break;
}
case MWNumericType.UInt8:
{
byte[] byteArray= (byte[])primesArray;
/* (Do something with byteArray . . .) */
break;
}
default:
{
throw new ApplicationException("Bad type returned
by myprimes");
}
}
}
}The code in the example also checks the dimensionality by calling NumberOfDimensions; see the following code fragment:
if ((!primes.IsNumericArray) || (2 !=
primes.NumberofDimensions))
{
throw new ApplicationException("Bad type returned
by mwprimes");
}
This call throws an exception if the array is not numeric and of the proper dimension.
You include functions from MATLAB APIs, such as the Engine API, in your C# code by using the DllImport attribute to import functions from libeng.dll (written in unmanaged C) and then declaring those functions as C# equivalents. The imported Engine functions are called using the P/Invoke mechanism, as illustrated in the example below.
For detailed information about using an IDE to build engine applications, see Specifying Engine Libraries and Files Required by Engine Applications in MATLAB External Interfaces.
Open Microsoft Visual Studio .NET.
Select File > New > Project.
Select Visual C# Applications in the left pane and Console Application in the right pane. Click OK.
Auto-generated code appears. Replace the auto-generated code with this code and run:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication8
{
class MatlabEng
{
[DllImport("libeng.dll")]
static extern IntPtr engOpen(string startcmd);
[DllImport("libeng.dll")]
static extern IntPtr engEvalString(IntPtr engine,
string Input);
public MatlabEng()
{
IntPtr engine;
engine = engOpen(null);
if (engine == IntPtr.Zero)
throw new NullReferenceException
("Failed to Initialize Engine");
engEvalString(engine, "surf(peaks)");
}
~MatlabEng()
{
}
}
class StartProg
{
public static void Main()
{
MatlabEng mat = new MatlabEng();
}
}
}
MWObjectArray, a special subclass of MWArray, lets you create a MATLAB array that references .NET objects. For detailed usage information on this class, constructor, and associated methods, see the MWObjectArray page in the NDoc (the MWArray Class Library). You can also search for MWObjectArray in the MATLAB Help browser Search field.
You can create a MATLAB code wrapper around .NET objects using MWObjectArray. Use this technique to pass objects by reference to MATLAB functions and return .NET objects. The examples in this section present some common use cases.
Passing a .NET Object into a MATLAB Builder NE Component. To pass an object into a MATLAB Builder NE component:
Write the MATLAB function that references a .NET type:
function addItem(hDictionary, key, value)
if ~isa(hDictionary,'System.Collections.Generic.IDictionary')
error('foo:IncorrectType',
... 'expecting a System.Collections.Generic.Dictionary');
end
hDictionary.Add(key, value);
end
Create a .NET object to pass to the MATLAB function:
Dictionary char2Ascii= new Dictionary();
char2Ascii.Add("A", 65);
char2Ascii.Add("B", 66);
Create an instance of MWObjectArray to wrap the .NET object:
MWNativeObjArray MWchar2Ascii=
new MWNativeObjArray(char2Ascii);
Pass the wrappered object to the MATLAB function:
myComp.addValue(MWchar2Ascii,'C', 67);
Returning a Custom .NET Object in a MATLAB Function Using a Deployed .NET Builder Component . You can also use MWObjectArray to clone an object inside a MATLAB Builder NE component. Continuing with the example in Passing a .NET Object into a MATLAB Builder NE Component, perform the following steps:
Write the MATLAB function that references a .NET type:
function result= add(hMyDouble, value)
if ~isa(hMyDouble,'MyDoubleComp.MyDouble')
error('foo:IncorrectType', 'expecting a MyDoubleComp.MyDouble');
end
hMyDoubleClone= hMyDouble.Clone();
result= hMyDoubleClone.Add(value);
end
Create the object:
MyDouble myDouble= new MyDouble(75);
Create an instance of MWNativeObjArray to wrap the .NET object:
MWNativeObjArray MWdouble= new MWNativeObjArray(myDouble);
origRef = new MWObjectArray(hash); Pass the wrappered object to the MATLAB function and retrieve the returned cloned object:
MWNativeObjArray result=
(MWNativeObjArray)myComp.add(MWdouble, 25);Unwrap the .NET object and print the result:
MyDouble doubleClone= (MyDouble)result.Object;
Console.WriteLine(myDouble.ToDouble());
Console.WriteLine(doubleClone.ToDouble());
Cloning an MWObjectArray. When calling the Clone method on MWObjectArray, the following rules apply for the wrapped object.
If the wrapped object is a ValueType, it is deep-copied.
If an object is not a ValueType and implements ICloneable, the Clone method for the object is called.
The MemberwiseClone method is called on the wrapped object.
Calling Clone on MWObjectArray
Optimization Example Using MWObjectArray. For a full example of how to use MWObjectArray to create a reference to a .NET object and pass it to a component, see the Optimization Example (C#) and the Optimization Example (Visual Basic).
MWObjectArray and Application Domains. Every ASP .NET Web application deployed to IIS is launched in a separate AppDomain.
The MATLAB .NET interface must support the .NET type wrapped by MWObjectArray. If the MWObjectArray is created in the default AppDomain, the wrapped type has no other restrictions.
If the MWObjectArray is not created in the default AppDomain, the wrapped .NET type must be serializable. This limitation is imposed by the fact that the object needs to be marshaled from the non-default AppDomain to the default AppDomain in order for MATLAB to access it.
When you access a complex array (an array made up of both real and imaginary data), you extract both real and imaginary parts (called components) by default. This method call, for example, extracts both real and imaginary components:
MWNumericArray complexResult= complexDouble[1, 2];
It is also possible, when calling a method to return or assign a value, to extract only the real or imaginary component of a complex matrix. To do this, call the appropriate component indexing method.
Tip Learn about creating type-safe interfaces for .NET components, in order to avoid data conversion tasks with MWArray. See Type-Safe Interfaces, WCF, and MEF for details. |
This section describes how to use component indexing when returning or assigning a value, and also describes how to use component indexing to convert MATLAB arrays to .NET arrays using the ToArray or ToVector methods.
The following section illustrates how to return values from full and sparse arrays using component indexing.
Implementing Component Indexing on Full Complex Numeric Arrays. To return the real or imaginary component from a full complex numeric array, call the .real or .imaginary method on MWArrayComponent as follows:
complexResult= complexDouble[MWArrayComponent.Real, 1, 2];
complexResult= complexDouble[MWArrayComponent.Imaginary, 1, 2];Implementing Component Indexing on Sparse Complex Numeric Arrays (Microsoft Visual Studio 8 and Later). To return the real or imaginary component of a sparse complex numeric array, call the .real or .imaginary method MWArrayComponent as follows:
complexResult= sparseComplexDouble[MWArrayComponent.Real, 4, 3];
complexResult = sparseComplexDouble[MWArrayComponent.Imaginary, 4, 3];
The following section illustrates how to assign values to full and sparse arrays using component indexing.
Implementing Component Indexing on Full Complex Numeric Arrays. To assign the real or imaginary component to a full complex numeric array, call the .real or .imaginary method MWArrayComponent as follows:
matrix[MWArrayComponent.Real, 2, 2]= 5;
matrix[MWArrayComponent.Imaginary, 2, 2]= 7:
The following section illustrates how to use the ToArray and ToVector methods to convert full and sparse MATLAB arrays and vectors to .NET arrays and vectors respectively.
Converting MATLAB Arrays to .NET Arrays. To convert MATLAB arrays to .NET arrays call the toArray method with either the .real or .imaginary method, as needed, on MWArrayComponent as follows:
Array nativeArray_real= matrix.ToArray(MWArrayComponent.Real); Array nativeArray_imag= matrix.ToArray(MWArrayComponent.Imaginary);
Converting MATLAB Arrays to .NET Vectors. To convert MATLAB vectors to .NET vectors (single dimension arrays) call the .real or .imaginary method, as needed, on MWArrayComponent as follows:
Array nativeArray= sparseMatrix.ToVector(MWArrayComponent.Real); Array nativeArray= sparseMatrix.ToVector(MWArrayComponent.Imaginary);
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes, as opposed to the elements of a non–jagged array whose elements are of the same dimensions and size.
Web services, in particular, process data almost exclusively in jagged arrays.
MWNumericArrays can only process jagged arrays with a rectangular shape.
In the following code snippet, a rectangular jagged array of type int is initialized and populated.
Initializing and Populating a Jagged Array
When adding fields to data structures and data structure arrays, do so using standard programming techniques. Do not use the set command as a shortcut.
For examples of how to correctly add fields to data structures and data structure arrays, see the programming examples in C# Integration Examples and Microsoft Visual Basic Integration Examples.
.NET Builder provides indexers to support a subset of MATLAB array indexing.
Note If each element in a large array returned by a .NET Builder component is to be indexed, the returned MATLAB array should first be converted to a native array using the toArray() method. This results in much better performance. |
Don't keep the array in MATLAB type; convert it to a native array first. See Getting Started for an example of native type conversion.
The MATLAB Builder NE product adds a WaitForFiguresToDie method to each .NET class that it creates. WaitForFiguresToDie takes no arguments. Your application can call WaitForFiguresToDie any time during execution.
The purpose of WaitForFiguresToDie is to block execution of a calling program as long as figures created in encapsulated MATLAB code are displayed. Typically you use WaitForFiguresToDie when:
There are one or more figures open that were created by a .NET component created by the builder.
The method that displays the graphics requires user input before continuing.
The method that calls the figures was called from main() in a console program.
When WaitForFiguresToDie is called, execution of the calling program is blocked if any figures created by the calling object remain open.
Tip Consider using the console.readline method when possible as it accomplishes much of this functionality in a standardized manner. |
Caution Use care when calling the WaitForFiguresToDie method. Calling this method from an interactive program, such as Microsoft Excel, can hang the application. This method should be called only from console-based programs. |
The following example illustrates using WaitForFiguresToDie from a .NET application. The example uses a .NET component created by the MATLAB Builder NE product; the object encapsulates MATLAB code that draws a simple plot.
Create a work folder for your source code. In this example, the folder is D:\work\plotdemo.
In this folder, create the following MATLAB file:
drawplot.m
function drawplot()
plot(1:10);
Use MATLAB Builder NE to create a .NET component with the following properties:
| Component name | Figure |
| Class name | Plotter |
Create a .NET program in a file named runplot with the following code:
using Figure.Plotter;
public class Main {
public static void main(String[] args) {
try {
plotter p = new Plotter();
try {
p.showPlot();
p.WaitForFiguresToDie();
}
catch (Exception e) {
console.writeline(e);
}
}
}
} Compile the application.
When you run the application, the program displays a plot from 1 to 10 in a MATLAB figure window. The application ends when you dismiss the figure.
As with managed code, any errors that occur during execution of an MATLAB function or during data conversion are signaled by a standard .NET exception.
Like any other .NET application, an application that calls a method generated by the MATLAB Builder NE product can handle errors by either
Catching and handling the exception locally
Allowing the calling method to catch it
Here are examples for each way of handling errors.
In the GetPrimes example the method itself handles the exception.
public double[] GetPrimes(int n)
{
MWArray primes= null;
MyPrimesClass myPrimesClass= null;
try
{
myPrimesClass= new MyPrimesClass();
primes= myPrimesClass.myprimes((double)n);
return (double[])(MWNumericArray)primes).
ToVector(MWArrayComponent.Real);
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex);
return new double[0];
}
}
In the next example, the method that calls myprimes does not catch the exception. Instead, its calling method (that is, the method that calls the method that calls myprimes) handles the exception.
public double[] GetPrimes(int n)
{
MWArray primes= null;
MyPrimesClass myPrimesClass= null;
try
{
myPrimesClass= new MyPrimesClass();
primes= myPrimesClass.myprimes((double)n);
return (double[])(MWNumericArray)primes).
ToVector(MWArrayComponent.Real);
}
catch (Exception e)
{
throw;
}
}
Note As of R2009b, native memory management for mxArray is automatically handled by .NET's CLR memory manager. There is no longer a reason to manually disable native memory management when working with mxArray. Calls to disable memory management will result in a null operation. |
Usually the Dispose method is called from a finally section in a try-finally block as you can see in the following example:
try
{
/* Allocate a huge array */
MWNumericArray array = new MWNumericArray(1000,1000);
.
. (use the array)
.
}
finally
{
/* Explicitly dispose of the managed array and its */
/* native resources */
if (null != array)
{
array.Dispose();
}
}
The statement array.Dispose() frees the memory allocated by both the managed wrapper and the native MATLAB array.
The MWArray class provides two disposal methods: Dispose and the static method DisposeArray. The DisposeArray method is more general in that it disposes of either a single MWArray or an array of arrays of type MWArray.
![]() | Basic Integration Tasks | C# Integration Examples | ![]() |

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 |