我们可以创建在C#GUI将运行在背手的MATLAB代码? [英] Can we create GUI in C# that will run the MATLAB code in back hand?

查看:469
本文介绍了我们可以创建在C#GUI将运行在背手的MATLAB代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计在MATLAB代码,它的正常工作。我在GUI在MATLAB工作,但它是一个头疼的问题我。我想我可以简单有效地创建GUI。我们可以创建在C#GUI,也将运行MATLAB的整个代码,我已经设计了??

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

推荐答案

是的,这是可能。有关详细信息,看一看:

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

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

如果你需要一个快速和肮脏的方式包装MATLAB代码与C#GUI(如的WinForms),一种选择是从您的MATLAB代码(.M)创建一个exe - 从.NET,你可以很容易开始此exe作为一个新的进程。注意,这种方法可能无法在某些情况下,最好的,怎么一回事,因为有一个EXE调用产生的延迟可能会相当可观(如对方的回答解释了)。

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

这是例子:第一,写MATLAB代码的函数:

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



输入参数将被传递给这个代码通过命令行字符串参数,因此,它们通过<转换code> str2num 。例如。在MATLAB通话

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

将被表示为

SamplePlot.exe 1 2 3 4

现在,创建.m文件一个独立的控制台应用程序:在MATLAB控制台,写:

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

deploytool

名称:SamplePlot.prj(例如)。目标:控制台应用程序。
添加.m文件。
套餐:加MCR(这是MATLAB编译器运行时 - 这是一个最终用户需要什么,如果他没有安装MATLAB;对于本地测试,您不需要添加此)。
然后使用:

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

最后,点击生成图标。一段时间后,会生成一个exe。现在,你就可以开始此exe从C#应用程序,例如一个过程按钮点击:

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.

(如果我没有记错,可以生成装配这种方式为好;然后,你可以调用组件,而不是一个exe文件)

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

这篇关于我们可以创建在C#GUI将运行在背手的MATLAB代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆