user2766220
user2766220

Reputation: 41

Can we create GUI in C# that will run the MATLAB code in back hand?

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??

Upvotes: 2

Views: 14276

Answers (3)

mjyazdani
mjyazdani

Reputation: 2035

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

Upvotes: 0

w128
w128

Reputation: 4928

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).

Upvotes: 4

sebastian
sebastian

Reputation: 9696

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.

Upvotes: 2

Related Questions