萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 3077|回复: 5

[电子] 在 matlab 中怎么引用c++

[复制链接]
发表于 2007-8-29 23:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
在  matlab 中怎么引用c++

[ 本帖最后由 eisenstange 于 2007-8-29 23:53 编辑 ]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-8-29 23:49 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-8-29 23:52 | 显示全部楼层
External Interfaces        MATLAB Data   Custom Building MEX-Files
Building MEX-Files

This section covers the following topics:

    *

      Compiler Requirements
    *

      Testing Your Configuration on UNIX
    *

      Testing Your Configuration on Windows
    *

      Specifying an Options File

Compiler Requirements

Your installed version of MATLAB contains all the tools you need to work with the API. MATLAB includes a C compiler for the PC called Lcc, but does not include a Fortran compiler. If you choose to use your own C compiler, it must be an ANSI C compiler. Also, if you are working on a Microsoft Windows platform, your compiler must be able to create 32-bit windows dynamically linked libraries (DLLs).

MATLAB supports many compilers and provides preconfigured files, called options files, designed specifically for these compilers. The purpose of supporting this large collection of compilers is to provide you with the flexibility to use the tool of your choice. However, in many cases, you simply can use the provided Lcc compiler with your C code to produce your applications.

The MathWorks maintains a list of compilers supported by MATLAB at the following location on the Web: http://www.mathworks.com/support/tech-notes/1600/1601.html.

      Note    The MathWorks provides an option called -setup for the mex script that lets you easily choose or switch your compiler.

The following sections contain configuration information for creating MEX-files on UNIX and Windows systems. More detailed information about the mex script is provided in Custom Building MEX-Files. In addition, there is a section on Troubleshooting, if you are having difficulties creating MEX-files.
Testing Your Configuration on UNIX

The quickest way to check if your system is set up properly to create MEX-files is by trying the actual process. There is C source code for an example, yprime.c, and its Fortran counterpart, yprimef.F and yprimefg.F, included in the matlabroot/extern/examples/mex directory, where matlabroot represents the top-level directory where MATLAB is installed on your system.

To compile and link the example source files, yprime.c or yprimef.F and yprimefg.F, on UNIX, you must first copy the file(s) to a local directory, and then change directory (cd) to that local directory.

At the MATLAB prompt, type

mex yprime.c

This uses the system compiler to create the MEX-file called yprime with the appropriate extension for your system.

You can now call yprime as if it were an M-function:

yprime(1,1:4)
ans =
     2.0000   8.9685   4.0000    -1.0947

To try the Fortran version of the sample program with your Fortran compiler, at the MATLAB prompt, type

mex yprimef.F yprimefg.F

In addition to running the mex script from the MATLAB prompt, you can also run the script from the system prompt.
Selecting a Compiler

To change your default compiler, you select a different options file. You can do this anytime by using the command

mex -setup

    Using the 'mex -setup' command selects an options file that is
    placed in ~/matlab and used by default for 'mex'. An options
    file in the current working directory or specified on the
    command line overrides the default options file in ~/matlab.

    Options files control which compiler to use, the compiler and
    link command options, and the runtime libraries to link
    against.

    To override the default options file, use the 'mex -f' command
    (see 'mex -help' for more information).

The options files available for mex are:

  1: matlabroot/bin/gccopts.sh :
         Template Options file for building gcc MEXfiles

  2: matlabroot/bin/mexopts.sh :
         Template Options file for building MEXfiles using the
          system ANSI compiler

Enter the number of the options file to use as your default
options file:

Select the proper options file for your system by entering its number and pressing Enter. If an options file doesn't exist in your MATLAB directory, the system displays a message stating that the options file is being copied to your user-specific matlab directory. If an options file already exists in your matlab directory, the system prompts you to overwrite it.

      Note    The -setup option creates a user-specific matlab directory in your individual home directory and copies the appropriate options file to the directory. (If the directory already exists, a new one is not created.) This matlab directory is used for your individual options files only; each user can have his or her own default options files (other MATLAB products may place options files in this directory). Do not confuse these user-specific matlab directories with the system matlab directory, where MATLAB is installed. To see the name of this directory on your machine, use the MATLAB command prefdir.

Using the setup option resets your default compiler so that the new compiler is used every time you use the mex script.
Testing Your Configuration on Windows

Before you can create MEX-files on the Windows platform, you must configure the default options file, mexopts.bat, for your compiler. The -setup option provides an easy way for you to configure the default options file. To configure or change the options file at anytime, run

mex -setup

from either the MATLAB or DOS command prompt.
Lcc Compiler

MATLAB includes a C compiler called Lcc that you can use to create C MEX-files. Help on using the Lcc compiler is available in a help file that ships with MATLAB. To view this file, type in the MATLAB command window

!matlabroot\sys\lcc\bin\wedit.hlp

replacing the term matlabroot with the path to the directory in which MATLAB is installed on your system. (Type matlabroot in the Command Window to get the path for this directory.)
Selecting a Compiler

The mex script uses the Lcc compiler by default if you do not have a C or C++ compiler of your own already installed on your system and you try to compile a C MEX-file. If you need to compile Fortran programs, you must supply your own supported Fortran compiler.

The mex script uses the filename extension to determine the type of compiler to use for creating your MEX-files. For example,

mex test1.F

uses your Fortran compiler and

mex test2.c

uses your C compiler.

On Systems without a Compiler.   If you do not have your own C or C++ compiler on your system, the mex utility automatically configures itself for the included Lcc compiler. So, to create a C MEX-file on these systems, you can simply enter

mex filename.c

This simple method of creating MEX-files works for the majority of users.

If using the included Lcc compiler satisfies your needs, you can skip ahead in this section to Building the MEX-File on Windows.

On Systems with a Compiler.   On systems where there is a C, C++, or Fortran compiler, you can select which compiler you want to use. Once you choose your compiler, that compiler becomes your default compiler and you no longer have to select one when you compile MEX-files. To select a compiler or change to existing default compiler, use mex -setup.

This example shows the process of setting your default compiler to the Microsoft Visual C++ Version 6.0 compiler:

mex -setup

Please choose your compiler for building external interface (MEX)
files.

Would you like mex to locate installed compilers [y]/n? n

Select a compiler:
[1] Compaq Visual Fortran version 6.6
[2] Lcc C version 2.4
[3] Microsoft Visual C/C++ version 6.0

[0] None

Compiler: 3

Your machine has a Microsoft Visual C/C++ compiler located at
D:\Applications\Microsoft Visual Studio. Do you want to use this
compiler [y]/n? y

Please verify your choices:

Compiler: Microsoft Visual C/C++ 6.0
Location: C:\Program Files\Microsoft Visual Studio

Are these correct?([y]/n): y

The default options file:
"C:\WINNT\Profiles\username\ApplicationData\MathWorks\MATLAB\R1
3\mexopts.bat" is being updated from ...

If the specified compiler cannot be located, you are given the message:

The default location for compiler-name is directory-name,
but that directory does not exist on this machine.
Use directory-name anyway [y]/n?

Using the setup option sets your default compiler so that the new compiler is used every time you use the mex script.
Building the MEX-File on Windows

There is example C source code, yprime.c, and its Fortran counterpart, yprimef.F and yprimefg.F, included in the matlabroot\extern\examples\mex directory, where matlabroot represents the top-level directory where MATLAB is installed on your system.

To compile and link the example source file on Windows, at the MATLAB prompt, type

cd([matlabroot '\extern\examples\mex'])
mex yprime.c

This should create the MEX-file called yprime with the .mexw32 extension, which corresponds to the 32-bit Windows platform.

You can now call yprime as if it were an M-function:

yprime(1,1:4)
ans =
     2.0000   8.9685   4.0000    -1.0947

To try the Fortran version of the sample program with your Fortran compiler, switch to your Fortran compiler using mex -setup. Then, at the MATLAB prompt, type

cd([matlabroot '\extern\examples\mex'])
mex yprimef.F yprimefg.F

In addition to running the mex script from the MATLAB prompt, you can also run the script from the system prompt.
Specifying an Options File

You can use the -f option to specify an options file on either UNIX or Windows. To use the -f option, at the MATLAB prompt type

mex filename -f <optionsfile>

and specify the name of the options file along with its pathname.

There are several situations when it may be necessary to specify an options file every time you use the mex script. These include

    *

      (Windows and UNIX) You want to use a different compiler (and not use the -setup option), or you want to compile MAT or engine stand-alone programs.
    *

      (UNIX) You do not want to use the system C compiler.

Preconfigured Options Files

MATLAB includes some preconfigured options files that you can use with particular compilers. The options files are located in the directory matlabroot\bin\win32\mexopts on Windows, matlabroot\bin\win64\mexopts on Windows x64, and matlabroot/bin on UNIX, wherematlabroot stands for the MATLAB root directory as returned by the matlabroot command. On Windows and Windows x64, the options files are named *.bat, where * stands for the compiler type and version. On UNIX, the options file is named *opts.sh, where * stands for mex or a specific compiler.

For a list of all the compilers supported by MATLAB, see the following location on the Web: http://www.mathworks.com/support/tech-notes/1600/1601.html.

      Note   The next section, Custom Building MEX-Files, contains specific information on how to modify options files for particular systems.

MATLAB Data        MATLAB Data                 Custom Building MEX-Files        Custom Building MEX-Files

&#169; 1984-2007 The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2008-9-20 14:42 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2008-10-8 01:56 | 显示全部楼层
原帖由 eisenstange 于 2007-8-29 23:52 发表
External Interfaces        MATLAB Data   Custom Building MEX-Files
Building MEX-Files

This section covers the following topics:

    *

      Compiler Requirements
    *

      Testing Your Config ...


基本上就是这样的。
给个简单的例子吧,“Hello Would!”

在matlab里面输入
edit
然后,复制粘贴下面的代码:
  1. //------------------------------myHelloWould.cpp----------------------------
  2. #include "mex.h"
  3. //---------------------------------------------------------------------------
  4. #define MY_LCC
  5. //---------------------------------------------------------------------------
  6. #ifndef MY_LCC
  7. void _export  mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  8. #else
  9. void  mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  10. #endif
  11. {
  12. printf("Hello %s !\n","Would");
  13. }
  14. /*----------------------------------------------------------------------------------------------*/
复制代码
完了,存贮为 "myHelloWould.cpp"
然后在matlab 里面输入:
>> mex myHelloWould.cpp
然后,就可以运行了。
输入:
>> myHelloWould
Hello Would !
>>

就可以了。

关于定义 MY_LCC 的原因:
由于matlab 有windows和linux不同的版本,所以,MY_LCC定义了用lcc(matlab自己的c编译器)或者gcc(linux 的常用编译器)编译的matlab c++ 程序。
但是,如果用户自己在windows里面,用VC 或者 BC 的编译器,就需要 remark #define MY_LCC 这一行。
因为,用VC 或者BC编译的matlab c++其实是一个dll文件。为了让mexFunction对 matlab可见,就需要用 _export  关键词定义函数。

[ 本帖最后由 recbio 于 2008-10-8 02:04 编辑 ]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2008-12-6 08:15 | 显示全部楼层

论文 创新性及难度

2. 创新性及难度
本文将参考管理决策方法与相关设计方法研究的成果, 试图寻找一套对于我国设计师来说,明确可行的跨专业设计计划的方法体系。
本文致力于从简明实效的角度硕士论文,为设计计划人员提供易于操控,而且便于和各个专业设计师进行沟通、交流的方法。要求该方法不仅对专业设计团队的计划环节有用,对个体设计人员的的设计工作也应具有指导作用。这就需要针对我国设计现状,从国内外各学科领域名目众多的相关方法中进行精心挑选,合理安排,科学综合的处理,创造出一套高效的计划方法来。虽然国外的相关成果业已成熟,但如何在众多不同侧重角度的方法中总结出理想的计划方法,需要我们对所有已知方法深入地认识和理解,同时明了我们设计各专业的工作规律,以期做到跨专业的有效性。

本文具有一定的难度。硕士论文首先在对计划的理论性分析与研究中,需要树立对计划的正确理解与认识,进而廓清设计计划的概念。接着将在设计计划方法论层面的研究上,对设计计划及其方法论进一步阐述。鉴于国内现在并没有对设计计划有深入的的系统的研究,该书内容基本上属此方面问题的首次讨论,硕士论文面临着缺乏大量相关经验及理论借鉴的景况,所以需要作者在目前积累的实践性资料和相关学科的研究成果中加以总结与深化。
本文最大的难度在于资料的搜集上,国内相关资料匮乏且本方向的研究缺乏交流的气候,而由于我院互联网情报系统的不完善和出于对技术保密的考虑,也很难从互联网上得到理想的资料。作者只能从书店,图书馆和其他专业的老师和同学手中求取所需要的信息硕士论文。当然本研究方向的直接信息是很缺少的,更多是从其它方向的研究成果中搜集所需要的信息资料。方法的研究是一个涉及面很广的课题,也需要从很多领域进行比较分,探索总结。而从一个学科到另一个学科的跳跃性研究,硕士论文需要迅速转换思维及反复调整视点,这也对作者的思维技能,思考方式,学术视野及知识积累等方面的研究素质提出了很大的挑战。
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-4-29 19:47 , Processed in 0.058151 second(s), 20 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表