Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to use matlab .dll in .net environment

Subject: How to use matlab .dll in .net environment

From: Antonio

Date: 19 Jan, 2010 18:20:30

Message: 1 of 3

Dear all,

 I am new in mathworks and this is my first post. I'm developing my first c# example application in VS2008 wich uses a matlab dll. For this reason, I've done in this way:
1. I've created a simple matlab file that sum two numbers.Then I've used the Deployment Tool to create the .dll library.
2. I've imported the .dll in my c# project, and I've add references to .dll and to MWArray.
3. I've add "using MathWorks.MATLAB.NET.Utility;" and "using MathWorks.MATLAB.NET.Arrays;" in my .cs file.
Now, how can I use my library in my program? Are there any references that explain the use of MWNumericArray or MWArray that I've found in different examples but I didn't understand?

Thanks a lot.
Antonello

Subject: How to use matlab .dll in .net environment

From: Antonio

Date: 20 Jan, 2010 10:49:03

Message: 2 of 3

Nobody?

Subject: How to use matlab .dll in .net environment

From: ExaltedRuler

Date: 25 Jan, 2010 16:50:21

Message: 3 of 3

I empathize, the documentation for .NET components is sparse.
Hope this helps.

Make sure:
 - copy "myComponent.dll" and "myComponentNative.dll" to your application directory.
 - execute the runtime installer: MCRInstaller.exe

Here's a shorty on mwarray:
http://www.mathworks.com/support/solutions/en/data/1-3NL1YX/index.html?solution=1-3NL1YX

Here's a snippet for parsing regionprops on a grayscale image returned to C# in a MWStructArray:

MWStructArray regProps = null;
...
    regProps = (MWStructArray)myMatlabClass.MyMatlabFunction( ... );

// PARSE //{ 'Centroid', 'Area', 'MeanIntensity', 'Eccentricity', 'PixelList', 'EquivDiameter'}

// Properties
int numRegions = regProps.Dimensions[0];

RawResults = new AnalysisResults(numRegions); // class def. is below

MWNumericArray centroidsTemp = null;
MWNumericArray AreaTemp = null;
MWNumericArray IntensityTemp = null;
MWNumericArray EccentricityTemp = null;
MWNumericArray PixelListTemp = null;
int numPairs;
double[,] PointsTemp; // to native double[,]
Point[] vector; // to Point[], each vector is different size
MWNumericArray EquivDiamTemp = null;

for (int region = 0; region < numRegions; region++) // foreach region
{
    // Centroid
    centroidsTemp = (MWNumericArray)regProps["Centroid", (region + 1)]; // not zero based indexing
    RawResults.Centroids[region] = new Point(centroidsTemp[1].ToScalarInteger(), centroidsTemp[2].ToScalarInteger());
    // Area
    AreaTemp = (MWNumericArray)regProps["Area", (region + 1)]; // not zero based indexing
    RawResults.Area[region] = AreaTemp[1].ToScalarInteger();
    // MeanIntensity
    IntensityTemp = (MWNumericArray)regProps["MeanIntensity", (region + 1)]; // not zero based indexing
    RawResults.Intensity[region] = (byte)IntensityTemp[1].ToScalarInteger();
    // Eccentricity
    EccentricityTemp = (MWNumericArray)regProps["Eccentricity", (region + 1)]; // not zero based indexing
    RawResults.Eccentricity[region] = EccentricityTemp[1].ToScalarDouble();
    // PixelList
    PixelListTemp = (MWNumericArray)regProps["PixelList", (region + 1)]; // not zero based indexing
    numPairs = PixelListTemp.Dimensions[0];
    PointsTemp = (double[,])PixelListTemp.ToArray(MWArrayComponent.Real);
    vector = new Point[numPairs];
    for (int pairIndex = 0; pairIndex < numPairs; pairIndex++)
        vector[pairIndex] = new Point((int)PointsTemp[pairIndex, 0], (int)PointsTemp[pairIndex, 1]); // float to int
    RawResults.blobVectors.Add(vector); // add to point array to list
    // EquivDiameter
    EquivDiamTemp = (MWNumericArray)regProps["EquivDiameter", (region + 1)]; // not zero based indexing
    RawResults.EquivDiameter[region] = EquivDiamTemp[1].ToScalarDouble();
}


class AnalysisResults
{
    public Point[] Centroids;
    public int[] Area;
    public byte[] Intensity;
    public double[] Eccentricity;
    public List<Point[]> blobVectors;
    public double[] EquivDiameter;

    public AnalysisResults(int numBlobs) // constructor
    {
        Centroids = new Point[numBlobs];
        Area = new int[numBlobs];
        Intensity = new byte[numBlobs];
        Eccentricity = new double[numBlobs];
        blobVectors = new List<Point[]>();
        EquivDiameter = new double[numBlobs];
    }
}

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us