How can I use a Collection.ArrayList in my C# application and pass it to the MATLAB .NET component
2 views (last 30 days)
Show older comments
I want to create an Collections.ArrayList in C# and pass it to a .NET component created with MATLAB Builder for .NET.
Accepted Answer
MathWorks Support Team
on 22 Jan 2010
You can use the ICollection.CopyTo method to make a shallow copy of the elements of the collection to a standard C# array. You can then cast this array to a MATLAB data type. Here is a simple exmple:
MATLAB code:
function ConsolePrinter(A)
disp(A)
C#-code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using ConsolePrinter;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Instanciate the MATLAB component
ConsolePrinterclass Pr = new ConsolePrinterclass();
//Initialize and populate a Collections.ArrayList
ArrayList list1 = new ArrayList(5);
list1.Add("Jody");
list1.Add("Red");
list1.Add("John");
list1.Add("Xiaoyu");
list1.Add("Black");
//Create shallow copy of the ArrayList alements to an array
string[] array1 = new string[5];
list1.CopyTo(array1, 0);
//print container element to screen
foreach (string i in array1)
{
Console.WriteLine("Element as native C# array:\n" + "\t" + i);
Console.WriteLine("Element as MathWorks type from MCR:");
Pr.ConsolePrinter(new MWCellArray(i));
}
//wait for user to exit
Console.ReadLine();
}
}
}
You may need to invoke IDisposible.Dispose method to garbage collect the collection.
This method will work for any of the System.Collections objects i.e. deque, queue, stack...
0 Comments
More Answers (0)
See Also
Categories
Find more on Deploy to .NET Applications Using MWArray API in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!