乡里高手要当不成了!Matlab(300分)

  • 主题发起人 主题发起人 o*o
  • 开始时间 开始时间
O

o*o

Unregistered / Unconfirmed
GUEST, unregistred user!
今天俺们邻村一个爱好编程的青年农民问了俺一个
Matlab问题,可是俺连Matlab长啥样都不知道,当
时满脸通红,镜片碎一地。

这个问题是:
怎样把.m文件转换成.mex文件,据说.mex是编译过
的二进制文件。这个.mex又如何执行?

俺可是乡里公认的农民电脑高手,大家务必帮忙!
 
hehe,
我将懒得翻译了。

?help mex

MEX Compile MEX-function.
All non-source code file names passed as arguments are passed to the
linker without being compiled.

Available options on all platforms are:
-argcheck Add code to check validity of input arguments to
MATLAB API functions (C functions only)
-c Compile only; do not link
-D<name>[=<def>] Define preprocessor macro name <name> [as having value
<def> (assignment not available under Windows)]
-f <file> Use <file> as the options file; <file> is a full path
name if not in current directory
-g Build a MEX-function with debugging symbols included
-h[elp] Help; print this message
-I<pathname> Include <pathname> in the compiler include path
-O Build an optimized MEX-function
-output <name> Create MEX-function named <name> (an appropriate
MEX-file extension is automatically appended)
-V4 Compile MATLAB 4 compatible MEX-file
-v Verbose; print all compiler and linker settings

On UNIX, the following options are available:
-l<file> Link against library lib<file>
-L<pathname> Include <pathname> in the list of directories to search
for libraries
-U<name> Undefine preprocessor macro name <name>
<name>=<def> Override options file setting for variable <name>

On Windows, the following options are available:
-setup Setup location of installed compiler. This
switch should be the only argument passed
-U<name> Undefine preprocessor macro name <name>

On the Macintosh, the following options are available:
<name>=<def> Override options file setting for variable <name>

MEX's execution is affected by both command-line arguments and
an options file. The options file contains all compiler-specific
information necessary to create a MEX-function. The default name for this
options file, if none is specified with the -f option, is "mexopts.bat"
(Windows), "mexopts.sh" (UNIX), and "mexopts" (Macintosh).

On UNIX, the options file is written in the Bourne shell script language.
The options file which occurs first in the following list is used:
./mexopts.sh, $HOME/matlab/mexopts.sh, $MATLAB/bin/mexopts.sh. Any
variable specified in the options file can be overridden at the command
line by use of the <name>=<def> command line argument. If <def> has
spaces in it, then it should be wrapped in single quotes
(e.g. OPTFLAGS='opt1 opt2'). The definition can rely on other variables
defined in the options file; in this case the variable referenced should
have a prepended "$" (e.g. OPTFLAGS='$OPTFLAGS opt2').

On Windows, the options file is written in the Perl script language. The
options file, mexopts.bat, is searched for in the following directories:
the current directory first, then the same directory as "mex.bat". No
arguments can have an embedded equal sign ("="); thus,

-DFOO is valid, but
-DFOO=BAR is not.

On the Macintosh, the options file is written in the MPW scripting
language for MPW C and Langauge Systems Fortran, and in M-code language
for Metrowerks Codewarrior C. The default location for the options
file is the <MATLABROOT>:extern:scripts folder. Any variable specified
in the options file can be overridden at the command line by use of the
<name>=<def> command line argument. If <def> has spaces in it, then it
should be wrapped in single quotes (e.g. OPTIMFLAGS='opt1 opt2'). The
definition can rely on other variables defined in the options file; in
this case the variable referenced should have a prepended "$"
(e.g. OPTIMFLAGS='$OPTIMFLAGS opt2').

不知你在哪个乡,我也想去混混了:)
 
呵呵,这都算乡里的,那俺们不就是原始人了?
 
首先俺代表乡亲们感谢4.13,乡亲们异口同声地说:
“一看4.13就是种Matlab这种庄稼的好把式,好象还留过洋哩。”

俺把这洋文交给那个青年农民,可怜她没等看完就晕过去了。
醒来气喘嘘嘘地说:“俺们农民斗大的字也认不了几斗,况洋文乎?
麻烦他们写些命令行的例子,详细说一下吧。”

俺们乡虽地处偏远山区,但是学习使用计算机的农民愈来愈多,
近来涌现出一大批计算机流氓。
乡亲们都说:“反正闲着也是闲着,就玩玩电脑吧!”
 
俺也听听老师讲啥子课
 
老师快讲!
 
把m文件转换(编译)成mex文件的命令是mcc:

mcc myfile

另外,还可以把多个文件编译成一个mex,
mcc file1,file2,file3,...
如果这些文件之间相互调用,那么主文件要放在第一个。
也就是说相当于Delphi中file1 需要 uses file2,file3,...
则file1必须放在第一个。

mex文件比.m文件有更高的优先级,同名的.m与.mex
文件在一个目录中时,matlab自动调用.mex文件,
因此似乎不必关心.mex文件如何执行。
 
的确是mcc, 但这个.m是Script M-File, 听说要转换为Function m-File.
请大虾们再指点一下。
 
你那位农民兄弟可能搞错了,MEX是matlab的扩展,
主要用来与C/fortran之类接口,而不是用来执行.m文件的。

matlab提供了.m-->.mex的编译,只有一种作用,就是把
functoin型的m-file编译,使之运行更快速(如果有循环之类)。
Script file与function file没关系,更不能转换。

你还是问问那位兄弟为什么要生成mex文件吧,恐怕就是为了实验?
 
唉,俺的农民兄弟上不了网,俺得通过村里的大广播喇叭招呼他:

“喂,韩老六!那个什么,这边温柔一刀老师问你为啥要生个mex文件?”

韩老六:“为啥?! 生个mex那个什么运算速度能翻上去好几成哩!”

“那个什么,温老师还说Script与function没关系,更不能转换。”

韩老六:“啥?俺看过HELP,说是能转,可就不说咋转!”

“俺说老六,你咋这么犟!还是在你家那两亩苞米地作实验吧,玩什么高科技!”

韩老六:“呸!老李家的老三,就兴你玩!全村人都听着哪,就这么寒惨俺!”
 
想当年俺在村里当广播站长的时候,大富翁还不知道张什么样哪!

(高声喝喊):俺不是说了吗?“只有一种作用,就是把functoin型的m-file编译,使之运行更快速”

因此老六说的没错,可是他说HELP讲script能转function,俺就不知道这是那一段Help了。估计
是老六同志操劳过度,再加上提速心切(想跟咱国家的火车比比,一年提好几次)因此理解有误。
我猜他一定看了这一段:“To make a SCRIPT file into a function, see FUNCTION.”
如果是这样,就算了吧,人家是说让你学会了function file怎么写,然后自己把script file
改写成function file,就可以编译了! :-)

手册上写得很清楚,想必也有例子,那就是script file 与function file根本是两个概念,
扩展名都是.m只不过是matlab自己的规定--matlab可执行文件都以.m开头。
 
老六留话儿了:
“看来俺不是那块料,种好苞米是真的。给温柔老师也来段洋文,把帐结了吧。”


3-12
****************
Converting Script M-Files to Function M-Files
****************
Converting a script into a function is usually fairly simple. To convert a script
to a function, simply add a function line at the top of the M-file.
For example, consider the script M-file houdini.m:

m = magic(2); % Assign 2x2 matrix to m.
t = m .^ 3; % Cube each element of m.
disp(t); % Display the value of t.

Running this script M-file from a MATLAB session creates variables m and t in
your MATLAB workspace.
The MATLAB Compiler cannot compile houdini.m because houdini.m is a
script. Convert this script M-file into a function M-file by simply adding a
function header line:

function t = houdini()
m = magic(2); % Assign 2x2 matrix to m.
t = m .^ 3; % Cube each element of m.
disp(t); % Display the value of t.
 
眼镜兄果然爽快,出手大方,幸亏我的答案也很“标准”:
“人家是说让你学会了function file怎么写,然后自己把script file改写成function file”

农民兄弟们以后有什么问题,尽管找我,
俺别的能耐不多,就是眼神比较好,不用戴o*o也能找到各种问题的答案 ^*^
 
后退
顶部