2

I have designed the code in MATLAB and it's working fine. I was working in GUI in MATLAB but it's a headache for me. I think i can create GUI simply and effectively. Can we create GUI in C# that will also run the whole code of MATLAB that i have designed??

Amro
  • 123,847
  • 25
  • 243
  • 454
user2766220
  • 41
  • 1
  • 1
  • 2
  • possible duplicate of [Run matlab from C# and give parameter to matlab](http://stackoverflow.com/questions/8857548/run-matlab-from-c-sharp-and-give-parameter-to-matlab) – Jay Elston Sep 10 '13 at 19:07
  • If your GUI is relatively simple, another way is to call a .NET modal dialog in Matlab and according to user's selection in the dialog, you can run a specific Matlab script. This method has a merit that you don't have to leave Matlab at all. – Tae-Sung Shin May 15 '15 at 20:15

3 Answers3

4

Yes, this is possible. For details, take a look at:

If you need a quick and dirty way to wrap MATLAB code with a C# GUI (e.g. WinForms), one option is to create an exe from your MATLAB code (.m) - from .NET, you can then easily start this exe as a new process. Note that this approach may not be the best in some situations, beacuse the delay introduced with an exe call can be quite substantial (as the other answer explains).

An example: first, write MATLAB code as a function:

function y=SamplePlot(p, d, w, t)
numericValueP=str2num(p);
numericValueD=str2num(d);
numericValueW=str2num(w);
time=str2num(t);

%... do stuff ...
plot(...);

Input parameters will be passed to this code as string parameters via command line, hence they are converted via str2num. E.g. a MATLAB call

SamplePlot('1', '2', '3', '4')

will be represented as

SamplePlot.exe 1 2 3 4

Now, create a standalone console app from .m file: in MATLAB console, write:

deploytool

Name: SamplePlot.prj (for example). Target: Console application. Add .m file. Package: add MCR (this is MATLAB Compiler Runtime - this is what an end-user will need if he doesn't have MATLAB installed; for local testing, you don't need to add this). Then use:

mbuild -setup

Finally, click 'build' icon. After some time, an exe is generated. Now, you can start this exe as a process from a C# application, e.g. on button click:

private void button1_Click(object sender, EventArgs e)
{
      string p=TextBox1.Text;
      string d=TextBox2.Text;
      string w=TextBox3.Text;
      string t=TextBox4.Text;
      string params = String.Format("{0} {1} {2} {3}",p,d,w,t);
      System.Diagnostics.Process.Start("SamplePlot.exe", params);
}

I left out some minor details, but this is one possible option.

(If I recall correctly, an assembly can be generated this way as well; you can then call the assembly instead of an exe file).

Community
  • 1
  • 1
w128
  • 4,680
  • 7
  • 42
  • 65
  • 1
    MATLAB has a product (MATLAB Builder NE) built on top of the MATLAB Compiler specifically to generate .NET assemblies: http://www.mathworks.com/products/netbuilder/. No need to mess with EXE files, you could use the generated assembly as any other .NET library. The result of course is dependent on the [MCR](http://www.mathworks.com/products/compiler/mcr/) – Amro Sep 11 '13 at 02:12
2

I'm pretty unfamiliar with C# but eventually happened to use .NET classes from MATLAB.

So, you could also do it the other way round, than the previous answers suggest:

Since MATLAB is able to create/open .NET gui-elements like dialog, I guess you should also be able to open your .NET-GUI from MATLAB an then plug in your MATLAB-Code via Callbacks. See e.g.: http://www.mathworks.de/de/help/matlab/matlab_external/getting-started-with-net.html

Depending on how frequently you want to execute matlab-code from your gui and how long the matlab-processing time usually is, this also avoids the pretty large overhead that's e.g. introduced by using a .exe generated with the MATLAB compiler. Say, you'd like to do quick matrix-calculation operations taking less than a second with every other button-click, than starting a standalone.exe everytime would make your gui pretty useless.

sebastian
  • 9,526
  • 26
  • 54
0

This link is so useful and simple: Call MATLAB Function from C# Client

mjyazdani
  • 2,110
  • 6
  • 33
  • 64