Main Content

Convert .NET Collections toMATLABArrays

Use theToArraymethod of theSystem.Collections.Generic.Listclass to convert a collection to an array. For example, useGetRangeto get three values from a list. Then callToArrayto create aSystem.Stringarray.

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®arrayDogs:

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

Now you can useDogsin MATLAB functions. For example, sort the array alphabetically:

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

Related Topics