【转】Matlab和C#混合编程

本文转载自:http://emuch.net/html/201311/6617105.html


在实验室算法演示和工程应用中存在这样的困惑:

1. Matlab精通各种数值计算和算法,但是没有优美的界面,更不用说对数据库访问操作。
2.Visual studio(C#)拥有完美的界面编程,窗口或网络编程已经优越的数据库访问等优势。
-------------------------------------------------------------
3. 国外有一本书叫:MATLAB C# Book图书(已经放到压缩包中了),看过以后收获不少。
4. 结合各种材料,我使用visual studio 2012+ matlab 2013a实现了从
Winfrom调用Matlab命令行绘图
Winfrom调用 .m文件编译的dll并在webBrowser中显示结果

调用Matlab命令demo

调用Dll demo

解决方案结构

压缩包中包含MATLABCSharp.pdf


谢谢各位捧场。
我附带把matlab配置过程也给大家上传上来。
【转载】终于学会C#调用matlab函数了,原来这么简单(也可以下载附件查看)
自己的配置:
(1)Microsoft Visual Studio 2005
(2)Matlab R2009a
(3)iis 6.0
第一步:
首先安装Matlab;
安装Visual Studio 2005或者更高版本;
安装MCRInstall.exe,我安装完Matlab之后在这里找得的:D:"Program Files\MATLAB\R2009b\toolbox\compiler\deploy\win32
点击:我的电脑-属性-高级-环境变量-系统变量-PATH-编辑,在变量值输入框中,不要删除以前的字符串,在最前面加入MCR的安装路径,如:D:"Program Files\MATLAB\MATLAB Compiler Runtime\v80\bin\win32; 然后确定、保存、重启电脑。(我就是不知道需要这两步,老是在C#创建Matlab的访问对象时,出现:“xxx的类型初始值设定项引发异常。”,完全不理解是什么原因,被耽搁了大量时间,直到安装了Matlab 2008b,仔细阅读它生成的readme.txt才知道)
第二步: 在matlab的Command window中输入mbuild -setup显示如下
>> mbuild -setup
Please choose your compiler for building standalone MATLAB applications: Would you like mbuild to locate installed compilers /n? n %选择n
Select a compiler:
Lcc-win32 C 2.4.1
  Microsoft Visual C++ 6.0
Microsoft Visual C++ .NET 2003
Microsoft Visual C++ 2005
Microsoft Visual C++ 2005 Express Edition
Microsoft Visual C++ 2008
None Compiler: 4 %选择4,其他编译器可以选相应的选项,我没有验证过
The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio 8, but that directory does not exist on this machine.
Use C:\Program Files\Microsoft Visual Studio 8.0 anyway /n? n%选择n
Please enter the location of your compiler:  D:\Program Files\Microsoft Visual Studio 8.0 %选择您自己的当前安装路径
Please verify your choices: Compiler: Microsoft Visual C++ 2005 Location: D:\Program Files\Microsoft Visual Studio 8.0 Are these correct /n? y %看上述信息,如果正确选择y
Warning: MBUILD requires that the Microsoft Visual C++ 8.0 directories "VC" and "Common7" be located within the same parent directory. MBUILD setup expected to find directories named "Common7" and "VC" in the directory: "C:\Program Files\Microsoft Visual Studio 8". Trying to update options file: C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R2009a\compopts.bat From template: D:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc80compp.bat Done . . . 到此matlab编译器设置成功。
第三步: 编写m文件:
function y=mymagic(x)
y=magic(x);
第四步:建立项目:
在matlab中点击“File- new -Development Project” 自己选择项目保存目录和项目名,如E:"和magicpro.prj 类 型选择.NET Component,如果你要生成更通用的COM组件,选择Generic COM Component。添加刚才的m文件到这个新建的项目中去。点击Build the project按钮(这个按钮的图标和微软开发工具的Build图标一样)或者右击选择“build”,等待3,4分钟。建立成功。
mcc -W 'dotnet:magicpro,Magicpro,0.0,private' -d 'E:\magicpro\src' -T 'link:lib' -v 'class{Magicpro:D:\work\mymagic.m}' Compiler version: 4.10 (R2009a) Processing include files... 2 item(s) added. Processing directories installed with MCR... The file E:\magicpro\src\mccExcludedFiles.log contains a list of functions excluded from the CTF archive. 2 item(s) added. Generating MATLAB path for the compiled application... Created 41 path items. Begin validation of MEX files: Sat Mar 27 09:22:22 2010 End validation of MEX files: Sat Mar 27 09:22:22 2010 Parsing file "D:\work\mymagic.m" (Referenced from: "Compiler Command Line"). Parsing file "D:\Program Files\MATLAB\R2009a\toolbox\compiler\deploy\deployprint.m" (Referenced from: "Compiler Command Line"). Parsing file "D:\Program Files\MATLAB\R2009a\toolbox\compiler\deploy\printdlg.m" (Referenced from: "Compiler Command Line"). Deleting 0 temporary MEX authorization files. Generating file "E:\magicpro\src\Magicpro.cs". Generating file "E:\magicpro\src\MagicproNative.cs". Generating file "E:\magicpro\src\readme.txt". Generating file "E:\magicpro\src\dotnet_mcc_component_data.cs". Executing command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /nologo /optimize /doc:"E:\magicpro\src\magicpro.xml" /t:library /r:"D:\Program Files\MATLAB\R2009a\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll" /resource:"E:\magicpro\src\magicpro.ctf" /out:"E:\magicpro\src\magicpro.dll" "E:\magicpro\src\Magicpro.cs" "E:\magicpro\src\dotnet_mcc_component_data.cs" Executing command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /nologo /optimize /doc:"E:\magicpro\src\magicproNative.xml" /t:library /r:"D:\Program Files\MATLAB\R2009a\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll" /resource:"E:\magicpro\src\magicpro.ctf" /out:"E:\magicpro\src\magicproNative.dll" "E:\magicpro\src\MagicproNative.cs" "E:\magicpro\src\dotnet_mcc_component_data.cs" Copying: E:\magicpro\src\magicpro.dll -> E:\magicpro\distrib Copying: E:\magicpro\src\magicproNative.dll -> E:\magicpro\distrib Copying: E:\magicpro\src\readme.txt -> E:\magicpro\distrib Compilation completed successfully. The output is located in E:\magicpro\distrib. You can package the component by clicking on the "Package" icon in the Deployment Tool toolbar, or by clicking the Tools->Package menu when the Deployment Tool panel is selected. To include additional files in the package, click Project->Settings).
第五步: 打开2005.新建网站 新建C#网页添加引用:
选择.NET,选择MathWorks,.NET MWArrays API字样的一个组件添加引用:选择浏览,选择刚才用Deployment新建的项目的E:"magicpro\distrib目录的magicpro.dll文件 . 设置页面,一个文本框(输入魔方的维数)一个按钮(提交),一个标签(显示魔方)。 代码如下:
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MathWorks.MATLAB.NET.Arrays;
using magicpro;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
    }
  

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        string html_value = "<table >";
        int input = Convert.ToInt32(TextBox1.Text);
        magicpro.Magicpro m = new Magicpro();
        MWNumericArray x = new MWNumericArray(input);
        MWArray argsout = new MWArray;
        MWArray argsin = new MWArray { x };
        m.mymagic(1, ref argsout, argsin);
        MWNumericArray y = argsout as MWNumericArray;
      
        TextBox1.Visible = false;
        Button1.Visible = false;
        for (int i = 1; i <= y.Dimensions; i++)
        {
            html_value += "<tr>";
            for (int j = 1; j <= y.Dimensions; j++)
            {
                html_value += "<td>";
                html_value += y;
                html_value+="</td>";
            }
            html_value += "</tr>";
        }
        html_value += "</table>";
        Label1.Text = html_value;
    }
}
运行输入4.点击按钮出现4阶魔方:
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1 成功

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值