How do I use the MWCellArray data type in a C# application with MATLAB Builder for .NET 2.1 (R2006b)?

5 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Feb 2010
The following example creates a simple .NET component that has a member function that in turn prints the contents of a cell array in MATLAB. To create and use this component from a Microsoft Visual Studio 2005 C# application follow these steps. This solution was written with MATLAB Builder for .NET 2.1 (R2006b). The exact steps may be slightly different for other versions of MATLAB or Microsoft Visual Studio.
1. Copy the following code into an MATLAB file named DispCellArray.m
function DispCellArray(mycellarray)
for i = 1:numel(mycellarray)
disp(mycellarray{i});
end
2. Open DEPLOYTOOL by executing the following command at the MATLAB command prompt:
deploytool
Note: If you are using MATLAB Builder for .NET 2.0 (R2006a) you will need to use DOTNETTOOL instead.
3. Select File->New Deployment Project and then select MATLAB Builder for .NET->.NET Component. For the project name specify CellArrayComp and click OK.
4. Click on the CellArrayCompclass folder, then click Project->Add File and add the DispCellArray.m file.
5. Select Tools->Build to create the .NET component. It will be located in a subfolder of the project directory called distrib.
6. Open Microsoft Visual Studio 2005 and select File->New Project. Then select Visual C#->Console Application and enter the name cellarrayex. Click OK.
7. Select Project->Add Reference. From the .NET tab select "MathWorks, .NET MWArray API".
Note: If you are using MATLAB Builder for .NET 2.0 (R2006a), you will need to upgrade to the latest MWArray assembly. You can download the file from here:
8. Select Project->Add Reference and from the Browse tab, navigate to the distrib directory where the CellArrayComp.dll file was created earlier. Select this file and click OK.
9. Open the file Program.cs and update it to be the following:
using System;
using System.Collections.Generic;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using CellArrayComp;
namespace cellarrayex
{
class Program
{
static void Main(string[] args)
{
//Create MATLAB object
CellArrayCompclass SpObj = new CellArrayCompclass();
//Create a cell-array
MWCellArray mwca = new MWCellArray(4);
Console.WriteLine("\n");
//display cell
mwca[1] = new MWCharArray("Hello");
mwca[2] = new MWNumericArray(54);
mwca[3] = new MWNumericArray(44);
mwca[4] = new MWCharArray("Goodbye!");
//pass MWCellArray to DispCellArray function
SpObj.DispCellArray(mwca);
//now display each cell individually
Console.WriteLine("Now display each cell individually");
Console.WriteLine("Cell 1 " + mwca[1].ToString());
Console.WriteLine("Cell 2 " + mwca[2].ToString());
Console.WriteLine("Cell 3 " + mwca[3].ToString());
Console.WriteLine("Cell 3 " + mwca[4].ToString());
}
}
}
10. Compile and run your C# project.
Note: MathWorks does not provide technical support for using Microsoft Visual Studio and can only offer assistance in how to use our API functions.

More Answers (0)

Products


Release

R2006b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!