MATLAB: Is there an example of using the “MWArray” data type in a .NET language such as C# with a MATLAB Builder for .NET component

arraycMATLAB Builder NEmwarraymwcellarraynativenet

With MATLAB Builder for .NET, I have built and deployed a .NET component. I now wish to use that .NET component in C#, but am having difficulty working with MATLAB's "MWArray" data type, typecasting to other "MW" data types, and converting the "MW" data types to native C# data types.
Is an example available which can help me?

Best Answer

This example was prepared using MATLAB Builder for .NET 2.1 (R2006b) on a 32-bit Windows XP SP2 platform. The compiler chosen via "mbuild -setup" is Microsoft Visual C/C++ (MSVC) version 8.0.
Procedure:
1. Create the following MATLAB function to deploy:
function out = cellexamp
num = 10;
array = [0 1;0 1];
str = 'Hello World';
out = { num,array,str };
2. Run "mbuild -setup", and select a Microsoft Compiler. (When preparing this example, Microsoft Visual C/C++ version 8.0 was chosen)
3. Run "deploytool", and create a project with the following settings:
MATLAB Builder for .NET -> .NET Component
Project/Component name: CellExample
Click OK
Class name: CellExampleclass
Add File: cellexamp.m
Under Settings -> Project Settings -> .NET -> Microsoft Framework, choose
-> "default" or 2.0 if using Microsoft Visual Studio 2005 (as in this example)
-> 1.1 if using Microsoft Visual Studio .NET 2003, which requires the .NET Framework 1.1.
Click Build
4. Open Microsoft Visual Studio (MSVS) 2005. Create a new project of type Visual C#, Console Application, named "celltest".
5. Add a new reference to MWArray.dll as follows:
a) Right click on "References" -> "Add Reference".
b) Click Browse and choose $MCRROOT\toolbox\dotnetbuilder\bin\win32\MWArray.dll (where $MCRROOT is the directory where MCR is installed)
Note that "MathWorks, .NET MWArray API" appears in the .NET list by default, but if you have multiple versions of MATLAB installed, this could point to the wrong version. Check the "Path" entry in the .NET list to be sure
6. Add a new reference to the compiled application as follows:
a) Right click on "References" -> "Add Reference".
b) Go to the tab ".NET", click Browse -> CellExample.dll (the .NET component just built by MATLAB Builder for .NET)
7.
a) In MSVS .NET 2003: Project -> celltest Properties -> Configuration Properties -> All Configurations -> Build. It may be necessary to set the "Output Path" to a local folder, not a network folder, to avoid security exceptions.
b) In MSVS 2005: Project -> celltest Properties -> Security.
If you are working in a network folder, it may be necessary to modify these settings to avoid security exceptions. Before making changes, please discuss with your system administrator the permissibility and implications of modifying security settings.
8. Replace the code in the default "Program.cs" with:
using System;
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;
using CellExample;
namespace celltest
{
class CellExampleApp
{
static void Main(string[] args)
{
MWCellArray cellout = null;
try
{
CellExampleclass obj = new CellExampleclass();
cellout = (MWCellArray)obj.cellexamp();
MWNumericArray item1 = (MWNumericArray)cellout[1];
MWNumericArray item2 = (MWNumericArray)cellout[2];
MWCharArray item3 = (MWCharArray)cellout[3];
Console.WriteLine("item1 is {0}", item1);
Console.WriteLine("item2 is {0}", item2);
Console.WriteLine("item3 is {0}", item3);
double[,] native1 = (double[,])item1.ToArray(MWArrayComponent.Real);
double[,] native2 = (double[,])item2.ToArray(MWArrayComponent.Real);
char[,] native3 = (char[,])item3.ToArray();
// Wait for user to exit application
Console.ReadLine();
}
catch (Exception exception)
{
Console.WriteLine("Error: {0}", exception);
}
}
}
}
9. Build and run the C# project.