Literature
首页医源资料库在线期刊成都医学院学报2006年第1卷第2期

Merging Programming of MATLAB and VBZHANG

来源:成都医学院学报
摘要:【摘要】ThispaperisaimedattherealizationofmergingprogrammingofMATLABandVB。WemainlydiscusshowtorealizemergingprogrammingbyMATLABAutomationServertechnology,anddemonstratehowtoincorporatevisualprogramminginterfaceofVBwiththepowerfulfunctionofMATLABinn......

点击显示 收起

【摘要】    This paper is aimed at the realization of merging programming of MATLAB and VB.We mainly discuss how to realize merging programming by MATLAB Automation Server technology,and demonstrate how to incorporate visual programming interface of VB with the powerful function of MATLAB in numerical calculation and graphic display.

【关键词】  MATLAB;VB;automation server

   1  Introduction

    MATLAB,developed by Mathworks co.,is a high-performance language for technical computing.It integrates computation, visualization,and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.Because of powerful matrix computation,it is called Matrix Lab.Matlab Language is similar with natural language.The usage of Matlab is extremely convenient.It also have plenty functions that can be called easy.But,it also has some shortcomings.Because the MATLAB language is a kind of explanation execution script language,it is very slow regarding the loops sentence execution.Under the same condition,compared to some high level languages such as Vc and VB,It executes loops sentences at the slow speed.The graphic user interface(GUI)of The MATLAB is not very friendly,and the parameter input and the output is not convenient.VB,as a kind of high level computer language,executes loops sentences more quickly,and its GUI is user-friendly.By incorporating visual programming interface of VB with the powerful function of MATLAB in numerical calculation and graphic display,we can take their advantages and avoid their weaknesses[1,2].

    2  Methodology

    There are two methods to merging programming of Matlab and vb.

    2.1  Transfer the Matlab functions into dynamic link library (DLL).Matlab provides a conversion tool,which can transform its own functions into DLL files by VC.

    The high level languages such as VB,VC and VB can call DLLs very easily.

    2.2  By some communication Technology between two different tools such as(Dynamic Data Exchange)DDE,Object Linking and embedding(OLE)automation and ActiveX controls.In this method,Matlab is taken as a object server and client can control Matlab by the associated methods of Matlab objects.

    Among the above mentioned 2 methods,Among above mentioned 2 methods,the process of the first method is more complicated,require the operator higher computer level,and it is also time-consuming.

    Compared to method one,the second is easy to carry out and have a lot of advantages.Therefore,in this paper,we mainly discuss method two.One interested in the other method can further read the reference.

    MATLAB can not only be the automation controller,can also be the automation server.In order to realize the call the function of MATLAB,one can take Matlab as the automation server,naturally,VB as the automation controller.

    Before calling one must at first know the name of the Matlab ActiveX objects in the system registry,namely,ProgID.Generally,the name is MATLAB.Application or MATLAB.Application.Single.

    The former means to automate MATLAB the server be a share of server,other procedures can adjust to use;

    The former means that the Matlab automation server is a shared server and other program can call its service.While,the latter means that Matlab is taken as a exclusive server and,if other program need to call its service,a new Matlab automation server have to run.The concrete realizations of the hybrid programming is illustrated as the following.

    2.2.1  Run the Matlab automation server

    In VB integrated development environment,the sentences to run Matlab automation server is as the following,Dim matlab as object

    Set Matlab=createobject(“matlab.application”)

    //Had better minimize the automation server after it run.

    MATLAB.MinimiZeCommandWindow(  ).

    2.2.2  Input and output of data

    In Matlab,there are two methods to input and output data,PutFullMatrix and GetFullMatrix.The definition of GetFullMatrix in Matlab is as the following[3,4],

    This function can transfer one or two dimensional arrays in Matlab workspace into VB program.According to its definition,VB can call this function of Matlab.For example,one need to transfer matrix A in default Matlab workspace into a 4X4 matrix in VB.

    The definition of PutFullMatrix in Matlab follows,

    Function PutFullMatrix can transfer the arrays variables in VB into Matlab matrix.For instance,the following sentence will load complex number with real part called Mreal and imaginary part Mimage into matrix A in Matlab workspace.

    an instance that demonstrates how to use these two functions as the following,

    2.2.3  Execute the Matlab sentences

    While the automation server is running,by calling the methods 'execute'of Matlab automation server,one can execute any sentences in Matlab.For instance,running the following codes in VB,

    x=0:pi/100:8*pi;

    y=sin(x)+cos(2*x);

    plot(x,y);

    title('Graph of X and Y');

    xlabel('X-Axis');

    ylabel('Y-Axis');

    //at first save the above codes into the LINES property of component MEMO1 and then run the following code

    FOR loop=0 TO Memo.Lines.Count-1

    MATLAB.Execute(Memo.Lines[loop]);// Run Matlab sentences line by line

    next

    the result is depicted in fig.1.

    Fig.1  the results(the left is the code and the right is the results.)

    2.2.4  Display the figures,created in Matlab,in VB

    Matlab have powerful ability in scientific drawing.VB can use Matlab to draw figures and then display it in VB proram[5].The codes is like,MATLAB.execute('figure(gcf)');//display the current figures created by MatlabMATLABn.execute('saveas(gcf,''c:/temp.bmp'')');// save the figures into temporary filesImage1.Picture.LoadFromFile('c:/temp.bmp'); // display the figures in IMAGE Component of VB.see the fig.1.MATLAB.execute('close'); // close the display windows created by Matlab.

    2.2.5  Close Matlab automation server.

    Having finished all calculations,in order to save system resources and memory,Matlab automation server should be closed.The method of closing is easy.Just one line,MATLAB.quit;Through the above five steps,we realized VB and Matlab mixed programming.This approach is simple and effective,suitable for use in a variety of applications.

    3  Conclusions and Discussions

    Matlab have poor performance when implementing cyclic sentences and its GUI is unfriendly.all these shortcomings of Matlab is just the advantages of VB.But the VB's ability of drawings is worse than Matlab.Mixing program will take good use of their advantages and make software development become more concise and efficient.However,we should note that the codes created by this method can run in computers with installing Matlab,which,to some extent,limits the portability of the system.the methods of removing this restrictions is to compile the concerned Matlab functions in to DLL(dynamic link library) files by VC++ and then call them in VB.

【参考文献】
  [1]Zhu Xiaosong,Guo Xiaoli. Discussions on VB and Matlab Mixing Program[J].Eletronic Technology Application,2003,29(009): 18-19.(in Chinese)

[2]Deborah's Rudimentary Home Page,(2006-1-14),[2006-11-24].http://www.djpate.freeserve.co.uk/MATLAB.htm.

[3]Liu Zhijian. The Users's Guide of Matlab Application Interface[M].Beijing:Science Press,2000.(In Chinese).

[4]Xu Wenshang. Matlab and VB Based Data Exchange and Integration Development[J].Industry Control and Computer,2003,16(11): 103-106.(in Chinese)

[5]Hu Zhiwen,Deng Tieyue,Yu Zengliang. The Integration between VB Applications and MATLAB[J].Computer Application,2003,12(2):200-203.

作者: Jun-peng1,CUI Yuan1,FENG Huan-ting2作者单位:1.De 2013-2-27
医学百科App—中西医基础知识学习工具
  • 相关内容
  • 近期更新
  • 热文榜
  • 医学百科App—健康测试工具