vb调用matlab问题
我在用VB调用MATLAB程序时,代码如下:<BR>Private Sub Command1_Click()<BR> Dim Matlab As Object<BR> Dim result As String<BR> Set Matlab = CreateObject("Matlab.application")<BR> <BR> result = Matlab.execute("zheng")<BR> <BR> result = Matlab.execute("print-dbitmap")<BR> Image1.Picture = Clipboard.GetData()<BR>End Sub<BR>图象显示为一个个彩色的方格,应该为一个一个正弦曲线呀。到底怎么回事,请各位大侠指导。急用呀。<BR>回复:(xueliang19)vb调用matlab问题
<DIV class=quote><B>以下是引用<I>xueliang19</I>在2006-3-29 19:10:47的发言:</B><BR>我在用VB调用MATLAB程序时,代码如下:<BR>Private Sub Command1_Click()<BR> Dim Matlab As Object<BR> Dim result As String<BR> Set Matlab = CreateObject("Matlab.application")<BR> <BR> <FONT color=#ff0000>result = Matlab.execute("zheng")</FONT><BR> <BR> result = Matlab.execute("print-dbitmap")<BR> Image1.Picture = Clipboard.GetData()<BR>End Sub<BR>图象显示为一个个彩色的方格,应该为一个一个正弦曲线呀。到底怎么回事,请各位大侠指导。急用呀。<BR></DIV><P>这是什么?</P> <P><FONT color=#ff0000>result = Matlab.execute("zheng")</FONT><FONT color=#000000><STRONG>此语句是调用</STRONG></FONT>文件“zheng.m”,文件“zheng.m”是生成一个正弦曲线的文件。不好意思,没有说清楚。</P>
回复:(xueliang19)vb调用matlab问题
<P>不懂vb,给你转个调用的例子吧<BR><BR> Dim strMatLab As String '定义matlab语句字符串<p></p></P><Palign=left> ' 显示正弦图<p></p></P>
<Palign=left> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<p></p></P>
<Palign=left> Dim MatLab As Object<p></p></P>
<Palign=left> '建立matlab应用<p></p></P>
<Palign=left> MatLab = CreateObject("matlab.application")<p></p></P>
<Palign=left> '最小化matlab命令窗口<p></p></P>
<Palign=left> MatLab.MinimizeCommandWindow()<p></p></P>
<Palign=left> 'matlab语句<p></p></P>
<Palign=left> strMatLab = "t=2:0.2:4*pi;y=sin(t);plot(t,y)"<p></p></P>
<Palign=left> '执行matlab算法<p></p></P>
<Palign=left> matlab.Execute(strMatLab)<p></p></P>
<Palign=left> '生成一个JPG的图形c:\Test1.jpg<p></p></P>
<Palign=left> matlab.Execute("print( gcf, '-djpeg', 'c:\Test1')")<p></p></P>
<Palign=left> '退出matlab,释放资源<p></p></P>
<Palign=left> matlab.Quit()<p></p></P>
<Palign=left> MatLab = Nothing<p></p></P>
<Palign=left> '显示在图片框<p></p></P>
<Palign=left> PictureBox1.Image = New Bitmap("c:\Test1.jpg")<p></p></P>
<Palign=left>End Sub<p></p></P>
<Palign=left><p> </p></P>
<Palign=left> ' 显示饼图<p></p></P>
<Palign=left> Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<p></p></P>
<Palign=left> Dim matlab As Object<p></p></P>
<Palign=left> matlab = CreateObject("matlab.application")<p></p></P>
<Palign=left> strMatLab = "sale=;pie3(sale,,{'公司A','公司B','公司C','公司D','公司E'})"<p></p></P>
<Palign=left> matlab.Execute(strMatLab)<p></p></P>
<Palign=left> matlab.Execute("print( gcf, '-djpeg', 'c:\Test2')")<p></p></P>
<Palign=left> matlab.Quit()<p></p></P>
<Palign=left> matlab = Nothing<p></p></P>
<Palign=left> PictureBox1.Image = New Bitmap("c:\Test2.jpg")<p></p></P>
<Palign=left> End Sub<p></p></P>
<P ><p><FONT size=3> </FONT></p></P>
<Palign=left> ' 显示三维螺旋线图<p></p></P>
<Palign=left> Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click<p></p></P>
<Palign=left> Dim matlab As Object<p></p></P>
<Palign=left> matlab = CreateObject("matlab.application")<p></p></P>
<Palign=left> strMatLab = "t=0:0.2:4*pi;plot3(sin(t),cos(t),t)"<p></p></P>
<Palign=left> matlab.Execute(strMatLab)<p></p></P>
<Palign=left> matlab.Execute("print( gcf, '-djpeg', 'c:\Test3')")<p></p></P>
<Palign=left> matlab.Quit()<p></p></P>
<Palign=left> matlab = Nothing<p></p></P>
<Palign=left> PictureBox1.Image = New Bitmap("c:\Test3.jpg")<p></p></P>
<Palign=left>End Sub<p></p></P>
<Palign=left><p> </p></P>
<Palign=left> ' 显示立体波浪图<p></p></P>
<Palign=left> Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click<p></p></P>
<Palign=left> Dim matlab As Object<p></p></P>
<Palign=left> matlab = CreateObject("matlab.application")<p></p></P>
<Palign=left> strMatLab = " = peaks(20);surfc(X,Y,Z);colormap hsv;axis ([-2 4 -6 8 -10 6])"<p></p></P>
<Palign=left> matlab.Execute(strMatLab)<p></p></P>
<Palign=left> matlab.Execute("print( gcf, '-djpeg', 'c:\Test5')")<p></p></P>
<Palign=left> matlab.Quit()<p></p></P>
<Palign=left> matlab = Nothing<p></p></P>
<Palign=left> PictureBox1.Image = New Bitmap("c:\Test5.jpg")<p></p></P>
<Palign=left> End Sub<p></p></P>
<P ><p><FONT size=3> </FONT></p></P>
<Palign=left> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<p></p></P>
<Palign=left> PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage<p></p></P>
<Palign=left>End Sub<p></p></P>
回复:(xueliang19)vb调用matlab问题
<P>利用ActiveX协议实现从VisualBasic6.0调用Matlab绘制一个温度场<BR>三维曲面图的例子,详细程序如下:<BR><BR>PublicSubMatlabTest(TempField() as double,<BR>TempWidth as Integer, _<BR>TempHeight as Integer, _<BR>Temp3DFile As String,<BR>TempContour File As String)<BR><BR>Dim Matlab As Object<BR>Dim Result As String<BR>Dim I() As Double<BR>Dim Width(1To1) As Double<BR>Dim Height(1To1) As Double<BR><BR>Set Matlab=CreatObject(“Matlab.Application”)<BR>Call Matlab.MinimizeCommandWindow()<BR>Width(1)=TempWidth<BR>Height(1)=TempHeight<BR>Call Matlab.PutFullMatrix(“w”,“base”,Width,I)<BR>Call Matlab.PutFullMatrix(“h”,“base”,Height,I)<BR>Result=Matlab.Execute(“[y,x]=meshgrid(1:1:h,1:1:w)”)<BR>Call Matlab.PutFullMatrix(“z”,“base”,<BR>TempField,I)<BR>Result=Matlab.Execute(“surf(x,y,z)”)<BR>Result=Matlab.Execute(“print f1 dbitmap<BR>c:\flame.bmp”)<BR>FileCopy“c:\flame.bmp”,Temp3DFile<BR>Result=Matlab.Execute(“Contour(x,y,z)”)<BR>Result=Matlab.Execute(“print f1 dbitmap<BR>c:\flame.bmp”)<BR>FileCopy“c:\flame.bmp”,TempContourFile<BR>Kill“c:\flame.bmp”<BR>Call Matlab.quit<BR>Set Matlab=Nothing<BR>EndSub</P> 用MatrixVB更好。谢谢大家!
对于我前些天写的那个程序,我知道毛病了,是出在print-dbitmap上面,原因是print和-dbitmap中间有个空格。由于粗心给写到一起了。不好意思,耽误大家的时间了。
页:
[1]