C# MATLAB混合编程
我附带把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 [y]/n? n %选择n
Select a compiler:
[1] Lcc-win32 C 2.4.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ .NET 2003
[4] Microsoft Visual C++ 2005
[5] Microsoft Visual C++ 2005 Express Edition
[6] Microsoft Visual C++ 2008
[0] 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 [y]/n? n%选择n
Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 9] 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 [y]/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
:\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[1];
MWArray[] argsin = new MWArray[] { x };
m.mymagic(1, ref argsout, argsin);
MWNumericArray y = argsout[0] as MWNumericArray;
TextBox1.Visible = false;
Button1.Visible = false;
for (int i = 1; i <= y.Dimensions[0]; i++)
{
html_value += "<tr>";
for (int j = 1; j <= y.Dimensions[1]; j++)
{
html_value += "<td>";
html_value += y[i, j];
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 成功
C# MATLAB混合编程的更多相关文章
- C++和MATLAB混合编程-DLL
先小话一下DLL,DLL是动态链接库,是源代码编译后的二进制库文件和程序接口,和静态链接库不同的是,程序在编译时并不链接动态链接库的执行体,而是在文件中保留一个调用标记,在程序运行时才将动态链接库文件 ...
- java matlab混合编程之返回值Struct类型
java matlab混合编程的时候当返回值是Struct类型(matlab中的返回类型)如何来取得(java中)其值? 上网找,看到这个网页:http://www.mathworks.cn/cn/h ...
- WPF(C#)与MATLAB混合编程
WPF(C#)与MATLAB混合编程 WPF可以为开发者提供便捷地构建用户交互界面的解决方法,而matlab则在科学计算方面有着无与伦比的优势,因此在一些需要将科学算法转换为应用软件的项目中,需要应用 ...
- VC 与Matlab混合编程之引擎操作详解
Visual C++ 是当前主流的应用程序开发环境之一,开发环境强大,开发的程序执行速度快.但在科学计算方面函数库显得不够丰富.读取.显示数据图形不方便. Matlab 是一款将数值分析.矩阵计算.信 ...
- VS/Qt C++和Matlab混合编程
最近两天在搞C++和Matlab混合编程,这个中间过程真是让人心酸啊,最后还是搞定成功!现在把这个过程记录一下. 首先自己的电脑本来就安装着matlab2013b,按着网上的说法首先需要输入!mcc, ...
- C++和MATLAB混合编程求解多项式系数(矩阵相除)
摘要:MATLAB对于矩阵处理是非常高效的,而C++对于矩阵操作是非常麻烦的,因而可以采用C++与MATLAB混合编程求解矩阵问题. 主要思路就是,在MATLAB中编写函数脚本并使用C++编译为dll ...
- matlab混合编程向导(vc,vb,.net...)
一.matlab与vc混编 1.通过mcc将matlab的m文件转化为cpp,c文件或dll供vc调用: 这方面的实现推荐精华区Zosco和ljw总结的方法(x-6-1-4-3-1和2) ...
- Matlab混合编程
Matlab混合编程 混合编程目的 在Matlab中采用混合编程目的主要包括 利用已有的函数库,避免重复工作 加速计算,特别是减少循环所用时间 利用GPU等进行异构编程 混合编程方法-mex函数 目前 ...
- C#Matlab混合编程类 初始化问题解决方法
************** 异常文本 ************** System.TypeInitializationException: “myPlus.matClass”的类型初始值设定项引发异 ...
- 国内第一部C#.Net调用Matlab混合编程视频教程
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 Matlab和C#混合编程文章目录:[目录]Matlab和C#混合编程文章目录 一.视频说明 2014年的5.1,我将这套视频教 ...
随机推荐
- django框架之中间件
中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在djang ...
- POJ - Problem 1275 - Cashier Employment
· 对于差分约束,题目要求求什么便设什么,令$Sum[i]$表示由$0 ~ i$的雇佣人数. · 要充分利用题目所给条件,令$Have[i]$表示i时刻申报的人数,$Need[i]$表示i时刻需要的人 ...
- python是如何进行内存管理的?
Python内存管理机制 Python内存管理机制主要包括以下三个方面: 引用计数机制 垃圾回收机制 内存池机制 引用计数 举个例子说明引用是什么: 1 如上为一个简单的赋值语句,1就是对象,a就是引 ...
- 在Ubuntu上安装Redis MySQL MongoDB memcached Nginx
1.安装Redis sudo apt-get install redis-server 2.安装MySQL sudo apt-get install mysql-server 3.安装MongoDB ...
- python redis-string、list、set操作
string操作 redis中的string在内存中都是按照一个key对应一个value来存储的 方法: set() 方法 : 写入一条数据 mset() 方法: 写入多条数据 , 可是Key-Val ...
- highcharts自定义导出文件格式(csv) highcharts的一些使用心得
highcharts是国外的一个图表插件,包括各种数据图形展示,柱形图,线性图等等,是手机端和pc端最好的图表插件之一,相比于百度的echarts更加轻便和易懂.链接http://www.hchart ...
- 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)
问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器
- 【hdoj_2100】Lovekey(大数+字符处理)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2100 根据题目意思,现将字符串转化为10进制,再采用10进制加法相加,再转化为26进制. 另一种直接的思路 ...
- Flume(一)Flume的基础介绍与安装
一.背景 Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步. 许多公司的平台每天会产生大量的日志 ...
- LoadRunner11设置场景百分比模式完成多台客户端压力测试
LoadRunner11用的不多,之前大部分的时候是用LoadRunner9.5,主要原因是由于担心新版本的稳定性,不过在Windows7系统下就不得不用LoadRunner11了,不过稳定不稳定,还 ...