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

北城以北 提交于 2019-11-26 21:44:22

问题


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


回答1:


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

  • Calling a MATLAB function from C#
  • Integrating MATLAB with C#
  • MATLAB Compiler

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




回答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.




回答3:


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



来源:https://stackoverflow.com/questions/18726775/can-we-create-gui-in-c-sharp-that-will-run-the-matlab-code-in-back-hand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!