tags:

views:

975

answers:

2

I want to pass an image file name as a parameter from C# to MATLAB. Here's what I have so far:

MATLAB code

function out = trial(im)
  O = imread(im);
  G = rgb2gray(O);
  imwrite(G,'output','jpeg');
  out = G;

C# code

private void btn_Browse_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
    }
}

When I browse and select an image file (openFileDialog1.FileName) I want to send it as the input parameter to the MATLAB function. How do I do this?

+1  A: 

For an external program to call Matlab you have to make use of the Matlab Engine, which is a standalone part of the Matlab suite. It is not possible to create a C# application and have it run code in the command window of Matlab.

A description of how to use the Matlab Engine is found here (examples given in C and Fortran).

Then for your application to run the code you want you have the choice of implementing this directly into your click event, or creating a Method and calling this method from the click event.

Jonas Hallgren
I also found this on codeproject: http://www.codeproject.com/KB/dotnet/matlabeng.aspx
Jonas Hallgren
A: 

As headbug has pointed there is a free and open source .NET wrapper for Matlab, and it is very simple: http://www.codeproject.com/KB/dotnet/matlabeng.aspx

I use this.

Jader Dias