Main Content

Convert .NET Collections to MATLAB Arrays

Use the ToArray method of the System.Collections.Generic.List class to convert a collection to an array. For example, use GetRange to get three values from a list. Then call ToArray to create a System.String array.

dog = NET.createArray('System.String',3);
dog(1) = 'poodle';
dog(2) = 'spaniel';
dog(3) = 'Irish setter';
dc = NET.createGeneric('System.Collections.Generic.List',{'System.String'},3);
AddRange(dc,dog);
temp = GetRange(dc,0,3);
dArr = ToArray(temp);

Create a MATLAB® array Dogs:

Dogs = {char(dArr(1)),char(dArr(2)),char(dArr(3))}
Dogs = 
    'poodle'    'spaniel'    'Irish setter'

Now you can use Dogs in MATLAB functions. For example, sort the array alphabetically:

sort(Dogs)'
ans = 
    'Irish setter'
    'poodle'
    'spaniel'

Related Topics