6 r7 O. K1 l8 Y, |: I2.3 MATLAB图像文件类型( y2 E' E) l5 Q$ P5 W
2 p4 c; `, S+ s- `
根据数据矩阵和图像象素点颜色的匹配关系,MATLAB中的图像可分为三类:索引图像、灰度图像和RGB图像。8 M; s; `$ @$ ?9 }$ P
; q! L; P+ V5 C4 Y* R# {1)索引图像:它的数据信息包括一个数据矩阵和一个双精度色图矩阵,它的数据矩阵中的值直接指定该点的颜色为色图矩阵中的某一种。色图矩阵中,每一行表示一种颜色,每行有三个数据,分别表示该种颜色中红、绿、蓝的比例情况,所有元素值都在[0,1]内。9 B& @" o* h. f; |" Y# W* Z
" s4 a/ P" N/ [2 a9 U* h- L' ~2)灰度图像:它的数据矩阵中的元素值一般都在[0,1]或[0,255]之间,灰度图像根据这些数据利用线性插值来和色图中的颜色种类匹配。 & ]' i* B( `4 `. K2 l4 g: o0 Z- X2 T. w0 i# i7 R/ G
3)RGB图像:图像中每个象素的颜色用三个数据来存储,分别指定红、绿、蓝三原色在象素颜色中的比例关系,组成一个三维数组。 B9 k' u5 v5 s' s
. t; l; \' h! J5 Q" Q- j) H2.4 MATLAB中图像的存储运算和显示方式 ) _! K) d6 U4 x2 r- f' k! s0 X 2 I/ R, s. I% _1 \- A- [ * {( E. w" o0 [2 Z4 c6 {
! f5 c% k; H# ~4 }2 j: V* C' G
在MATLAB中,数值一般都采用double型(64位)存储和运算,为了节省存储空间,MATLAB为图像提供了特殊的数据类型uint8(8位无符号整数),以此方式存储的图像称为8位型像。函数image能够直接显示8位图像,但8位型数据和double型数据在image中意义不一样,对于索引图像,数据矩阵中的值指定该像素的颜色种类在色图矩阵中的行数。当数据矩阵中的值为0时,表示用色图矩阵中第一行表示的颜色绘制;当数据矩阵中的值为1时,表示用色图矩阵中的第二行表示的颜色绘制该像素,数据与色图矩阵中的行数总是相差1。所以,索引图像double型和uint8型在显示方法上没有什么不同,只是8位数据矩阵的值和颜色种类之间有一个偏差1。调用格式均为image(x); colormap(map);对于灰度图像,uint8表示范围[0,255],double型表示范围[0,1]。可见,double型和uint8型灰度图像不一样,二者转换格式为: 3 a( h/ }( A( h u% }, p7 v9 r- j) m! H- N! l4 Y
I8=uint8 (round (I64*255)); ( U: w# O$ d; y& _ 5 w3 J. S1 |" }6 y4 L7 CI64=double (I8)/255;& L" f$ O5 M1 N( t- l' L& {! _2 P
+ t2 _ i L; v {. d
反之,imread根据文件中的图像种类作不同的处理。当文件中的图像为灰度图像时,imread把图像存入一个8位矩阵中,把色图矩阵转换为双精度矩阵,矩阵中每个元素值在[0,1]内;当为RGB图像时,imread把数据存入到一个8位RGB矩阵中。 , N' k" H) i! B, y2 n9 g" _4 j8 T) L- q9 q
本软件的处理统一针对BMP格式的灰度图。在编程时图像读入与输出均要求转换为uint8型,中间运算处理过程则要用double型。 ( T4 a1 Q! S7 X9 ]5 Y4 T6 h) q {# F4 t4 p* z% { M5 ]
2.5 从零开始学用MATLAB! s; A: a, o9 ]9 }( a
6 S& g. Y. P% B4 b3 c# T ' W, F% H8 g0 s( U9 G1 d ; d; y) f& t8 r% k2 ^3 P MATLAB对于我来说是一门全新的语言,一般在编辑框中编程存盘后在命令框内运行,在Figure框中显示图像。在用它作数字图像处理的过程中,最常碰到的问题有:( O' b; y9 A6 f" [ s
" O, S( l4 {" i! d( l( D# a① 图像读入 imread (‘文件名.格式’,’格式’),必须是上文提过的MATLAB支持的7种格式之一,而用Photoshop制作的PSD图像保存了所有通道和图层的信息,不能直接用MATLAB来处理。显示图像用imshow (h) 语句,h 为图像句柄;在一幅图片中显示子图用subplot规定子图的位置和大小,用subimage(h)输出子图。输出图像若需要永久保存,则可用imwrite (h,map, ’filename.bmp’,’bmp’), 写入存储器。注意在该语句前要设置调色板,即map=(gray(256))。8 V5 S4 X @3 X2 ?0 r
' \% `3 }: a" i* `
② 结构化程序语句写法不同于C语言。比如if, for 之后要用end 表示这一块结束。有关条件的语句都不用加括号,而用冒号来表示递增,如1 H' a! i" }- e4 B- R2 ?3 R! @0 y( a
1 _1 u% K3 J: bdbquit: ) U- c3 d2 V( R J) V* m" ?+ C * b; _! G# {5 u1 Y. l
5 ~/ U9 [# w7 J0 O/ s
退出调试模式 - y4 @" W+ c9 |' J" x# s , O5 F$ f X' z* D5 B- g$ |dbstack:' I) H4 H& t+ n8 a8 R( b* M j
7 Y; N' J, Z% ?/ i: \7 R. K / `6 y/ n0 p" p6 U' E列出谁调用谁' e1 R2 N7 Y# T# \, Y& F
: M1 {" g) X3 ^8 I E1 wdbstatus:6 j7 W8 e' f. N D5 T
5 g, e( _4 _) C* K$ u8 l
4 s& J3 n3 Q6 Q7 F, p8 G$ c
列出所用的断点 / b; G5 ?7 n$ j9 l! d- W, J+ K6 G5 B. w3 ?! W W# x
dbstep:# u) r) P) V V' b. V
# Y# |$ d( U& }& I% \; T其实也出来很久了,不知大家有没有注意到.9 `% w# o, l4 ^- \1 K7 c5 @+ }
' n. O" n1 `" B1 P$ }: F4 c
虽然是针对繁体中文系统的,我试过在简体 ) ]9 O% m" x* K2 \/ I$ A i$ f7 C& Y: V) O
中文系统下一样可以用。 6 X. n: V* k' Z6 I e/ H& s" ^ % u% Y4 ]6 U; Q! f- E* Xhttp://www.mathworks.com/support/solutions/data/26985.shtml % @2 r6 j; a$ y3 x5 ~, Q8 m' m. W% J http://www.mathworks.com/support/solutions/data/26990.shtml. t+ z! C: {' S- h% X% K
. L Q4 K% v9 i6 S, g
Solution Number: 26990 & [' [3 i6 Y" X) |" P 3 ]/ |2 a4 }% H K. ^( ^6 ~+ ~Date Last Modified: 2001-01-301 \' E8 h4 J: w5 j" n4 Q
, x- u) D) |4 E% \' R( R
Product: MATLAB 6.0 ==> Current Version) n0 v5 ~3 m( |4 D9 m' ?- R
5 A, O- D; F8 B+ v! W# @Platform: Windows , d z" \% y+ n" ~" Q% y7 A; N3 g* e3 e$ p
Problem Description. K2 | B3 m3 E# ?8 L
# K8 Q% O( I, s5 t/ _6 d7 o
Why do I encounter problems when running MATLAB 6.0 (R12) on Hebrew8 j" C6 _' b4 \5 [4 W9 t
( T( P9 o W: N7 t( p! `4 `& x9 uor 7 C$ d1 `9 Y! x6 L+ T : t T$ a8 m. R# wTraditional Chinese (Taiwan) Windows? I try to start MATLAB but after# v% c9 X) M( x& W2 `8 H+ L5 _
* m6 [5 x% R v
the splash screen disappears, MATLAB exits.7 q* b9 t, k; [: \4 y, r" z. l
: I, P# q/ o! b. {: I* s# PPLEASE NOTE: This solution only applies to MATLAB 6.0. If you have a * e# E9 T/ n5 ^$ r+ R / B# L* A, E. Bsimilar problem with MATLAB 5.0 or the Student Edition of MATLAB 5.0,& N- K3 m' o, X3 p
- T. \" G) Q, j+ W/ v) asee solution 7213.5 V% o3 ], a4 z6 Q7 \( R
1 f Y6 R. {/ |This problem is caused by a bug in one of the font properties files we ship 9 R6 ?. O6 x7 @& t. ~0 z& ^. ?% n, B/ z* X' C
with MATLAB. The font.properties file is used by Java to map the standard* q3 V% R4 u) P0 `
, p C9 p, O; P! R2 y' x" k1 Q7 JJava font names to system fonts for a particular language operating system.! W2 o( R+ Q" T' f+ Q. C
& D- O& s0 a- @3 e" ^However, we made a few assumptions that do not hold for the Hebrew or. m% E# c8 f5 v* k! W! A3 ?$ j
K3 s# J& |. m% LTraditional Chinese Windows, causing this problem.' O6 l4 z$ I" f9 N9 z
* [& G4 K) S |. |* aWe have created a fixed version of the mwt.jar file that you can use to8 l0 M( U; h/ w+ M
. F7 n# n. [" [! k. h: F/ K
correct this. To use the fix, first rename your mwt.jar file as mwt.old.1 j, Q' I5 M/ W& u. r1 C7 I; z
, A/ A- P% T. V9 O% H* a$ r
This file is found in the $MATLAB\java\jar directory, where $MATLAB is your+ N5 {+ o- {8 |. ?$ K
' L) r" k+ d* j: R- s
MATLAB root directory. Then download the newer mwt.jar file from: q' Y4 V$ [! u3 Q $ p- L: x1 ?9 i- G$ Fftp://ftp.mathworks.com/pub/tech-support/solutions/s26990 ; g O! h+ p; N0 Y( E$ ]9 L ! `' [2 {0 U9 v7 o7 L# dand place it in your $MATLAB\java\jar directrory. Then restart MATLAB;; Z6 o: d' ~4 m: B6 p9 l
! k& C p4 V) C0 k
this should correct the problem you're seeing. * O; H) R) H. r+ z9 c$ y6 p% p! S8 O n
2)我有一组x,y,z值,非规则排列,如何在Matlab中绘图?! h# g" S; {9 Q+ X% J) D0 {4 |
9 T. b$ [% t3 A: g0 L" k
:#FangQ(Qianqian.Fang@Dartmouth.Edu),2002/6/12, BigGreen/MathTools # 8 ~/ p& Z$ |, I# e0 \ [' T! a1 X- b k
参见第一节问题7)2 R9 j3 p& P) q
$ |; I- ~ j1 E3 l' g
3)如何在给定句柄的axis里绘图? - P/ e Y1 {# y* t 7 \5 T: \4 @$ B1 m; A9 \:#FangQ(Qianqian.Fang@Dartmouth.Edu),2002/6/12, SMTH/MathTools #1 R' R8 t% S6 G" Z5 X8 I/ t; s3 `
: `+ j) U" U: Z, ]! s: ~
plot(data,'parent',haxis);; G1 J9 ^0 n* R$ U4 m* t
7 x; Y7 v! i% ^: a+ u: U1 {; U
或者6 I0 C( H+ L+ p `. A/ B
! }- k8 S: \+ U6 {, d" H! YY={y1,y2,y3,…,yn}' / _' e3 Z2 ^9 m) c; a, _, c- F4 r ) Q# O9 @. L7 s则系数{a1,a2,…,am}'=pinv(A)*Y 6 F* P1 ^. T. B# Y5 F- B& D+ m9 K8 N
在matlab中使用 2 e, I7 f' w; {$ \3 q/ {( L. N* f9 n0 V+ }4 `% m' e
coeff=A\Y 6 w% K7 x8 Z; z9 I" x9 e" M5 V ! A5 K$ W8 z& H6 x9 s7 p则可以得到最小二乘意义上的拟合系数 $ r3 B2 s* q5 ]. l m' r3 ~. }9 V' L% G* g6 O) P% d
7)Matlab中如何作圆回归? # P& Z% o. P) Q7 Y- u3 ^ 2 t; g* U9 p+ \" @:#Peter Boettcher (boettcher@ll.mit.edu),2002/5/16, comp.soft-sys.matlab# A/ p, h& p% d/ i/ g9 b
+ k% m0 w8 X/ f* o7 [1 QQ5.5: How can I fit a circle to a set of XY data? 1 \+ D! B4 K% P0 t1 Y! T( C 6 g+ E1 C! [* N! r7 F================================================= 7 v- s) Z3 |( ^. m! h + B5 X7 H* O1 [- h f) c7 [5 x) [An elegant chunk of code to perform least-squares circle fitting was5 D/ k2 R" ]6 x, X
) a, e: A3 [! V9 _& \written by Bucher Izhak and has been floating around the newgroup for" n1 d7 h( `$ N3 G! s
& u. U p b+ S$ G
some time. The first reference to it that I can find is in: 9 H* w9 q4 L" S C+ M( G& z7 j 9 x' j1 s" d) R1 _2 ?function [xc,yc,R,a] = circfit(x,y) 7 r, n' t$ S5 N/ ` ' i' Y; p$ p, U/ M; |& j: W%CIRCFIT Fits a circle in x,y plane$ o0 d4 u8 i; ]6 a
8 ~! p( L) ^- N6 z4 _7 V5 Q0 t2 F%' O F2 D: D6 B8 V
3 p( [% c; ?* ?" z- B" J$ l
% [XC, YC, R, A] = CIRCFIT(X,Y)* Z2 @1 l) q& _. ?
' R. C/ m6 M7 T4 _9 W6 Q
% Result is center point (yc,xc) and radius R.A is an optional* N3 \& z: g* H( a. {' ~
7 y0 p& ]% q& _* H) ?1 m
% output describing the circle's equation: 2 T6 C- l" D9 d/ u7 c % N2 w: a" m: C, D. z ^# O v6 i9 R%) i' P( `: q/ g y/ h
. n- e( }$ J6 x
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0) J9 G7 g7 M+ z: z0 b4 R
* B& ^' @4 U$ q- a }9 `
% by Bucher izhak 25/oct/19916 V6 ?. O4 U, b2 d
9 W" g8 c' \4 {1 S/ [3 V
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y; d" f. t' t, V6 C0 {( s& ^/ _- G9 u# ?3 x9 [
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];6 h; _& [3 w+ R$ o
- O, i- f' @* {$ r* @
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];/ p0 _8 I. q9 [+ w) j
) A' p; h( | u! }/ e: I" Z9 d9 Y
a=A\B;! c0 k) p# j( Q' v0 k3 }
+ S, U0 u; y/ ?8 V# k' Z
xc = -.5*a(1); % f3 r# f; |# x, I! y 4 A" Z* ^9 H. L* S# j% jyc = -.5*a(2);, G) h* Z4 C1 E1 ~5 ]( _9 V z
8 H( g$ W0 ?/ Y: G
R = sqrt((a(1)^2+a(2)^2)/4-a(3));) m" _- J2 l4 n5 J! k4 _
1 L- J6 R$ e, H
Tom Davis provided a more sophisticated approach that works for more6 E( V/ L4 |/ w' _ x' i6 \8 ~- M
% C, }0 z) b9 ], Wcases in and Code included.7 y9 _" K0 C# @5 L7 z
5 O0 C6 J2 W d8)Matlab中如何绘制箭头? / X8 [' A5 o0 n3 I3 t ! a5 v2 O5 S' ^3 P7 r:#FangQ(Qianqian.Fang@Dartmouth.Edu),2002/6/21, SMTH/MathTools ## u1 E' b% ?" @; q- T
- P5 U5 j$ e' p: p* k20)Matlab中能开的最大数组是由什么决定的?+ u0 g$ G3 a! y# v3 F2 Q3 J8 I9 U
' R C% A9 h- c* K) N. {
:# chenft (mike),2002/6/1, SMTH/MathTools #- c) m& g& u, g. a
6 I* ~! Z4 D( Y& M, M4 F7 jI have had similar problems. Below is an explanation I received from Ian0 g- o5 m8 |/ m& t
. M0 z1 c+ e k% `; o4 x. B6 k. }Boyd. ]1 h( F0 p6 Y8 v3 q! S- @; v
6 E0 B3 |( X* H& |8 `+ w
from Mathworks (just giving credit where credit is due) that explains % f* Y2 O3 k% v0 |( }8 Z 5 I( m4 H5 t7 `! }5 D) ~what's happening. You solution is to run matlab with the -nojvm mode.1 c8 r/ t1 |/ z B+ [( j: _2 r7 B
; m; [% K7 X9 W' G$ w3 H* V3 }
"The heap memory system in J***A consists of data and handle elements.2 E8 |4 V# x8 t
( h1 Q+ x) y, ]
When you allocate a variable you get a handle and data. As long as data0 g' i, V) C4 |2 ~
, b9 A4 L' C; b/ |: Z" yhas an ! Q+ e' X( [/ H! W0 f, x# A: d4 ~" ^2 L5 Y# N
associated handle, the JVM considers it valid and will not clean it up.; q; O; @$ \7 d
. h" g" U3 \% _# Q+ F! Q2 zHowever, when you call the clear function in MATLAB, all handles are 1 r( l2 X: Z& J/ e' r0 l+ p ! V a {+ B) K. _0 x7 Cdestroyed, and the data associated is now invalid. This means that the ' D/ X, `4 p- W; s) d' V/ l! z3 V
J***A+ J; t; \. C& \9 m/ U; Q0 r
; ?2 S/ D5 W8 S6 F; Y* \
engine can free up that data (garbage collection), but does not mean # P2 k2 _& X) i& A1 ~9 u- R% ~/ B1 w" }) S- u
that it will clean it up at that moment.& S& D& f3 r, l
0 p, ^9 J( W8 J# N! xCalling the PACK command encourages J***A to run the garbage collector5 B9 X. z2 s; S+ x( \8 T
+ R6 h! K5 N6 _
and de-fragment the memory. But it does not force it to (This is part( r8 Z% S1 x: N
! x. V( j2 A* d( X4 U
of the J***A design). Even though the memory is 'freed' on the heap, ' f) i: g2 w& M& D7 B / ~3 o: b8 _- T' o5 A% w4 mit is not actually free to the OS, it is only free to the JVM. Here % _( T; ]3 }3 ?) Z/ { ^& {! r % W' y" J6 S5 iis one way to think of it:( q8 u! k$ J2 P" \+ u: | Q
' M4 F8 X! L' @3 J Q! T/ x[MATLAB]0 l0 F, M7 g/ n/ Y% j
4 p" d$ K w- x2 H6 P T8 s
[J***A]% V' m; a" ` `! p: X1 a
9 c2 u _- H. f* l
[OS]) n+ G: ^" H0 Z1 b8 z4 c0 X
% O: n# @, D# S; s4 C3 _MATLAB runs on J***A (virtual machine), and Java runs on the OS (physical7 B, ^: V' q8 Q2 x
) {5 a* c3 H5 E! q, m+ {machine). So when MATLAB is running in J***A mode memory allocations1 y% ?, J5 t/ ~4 o) {- N
/ S8 I3 l* D( k" o7 o9 U5 Qare requested from the JRE, not the OS. 5 ]' D5 v7 I5 U+ ?; [ % y9 I3 P9 }; `One problem you may be running into is that the default maximum J***A heap * D2 ?2 M: H9 }/ R4 D+ Q% j1 u* K1 A& b+ ?3 X; e4 h
size is relatively low ( <= 64 M, so that is all the memory one session * X/ i# E3 g3 [7 U! z o' i" m' f2 q1 V% Sof MATLAB will ever get on your system.8 m( P% b! g& ?% O8 i9 m; t: S/ W, |
9 H8 i7 R W2 B, \
The good news is that you can increase this value. You will need to create" A* \: `: d1 ?0 x+ u% x" Q! X
3 k# k" _6 S3 P: `" ~5 d, u
a java.opts file in $MATLAB/bin/$ARCH (or in the current directory when1 e! C- ~4 P( ~. e
) B) p; @/ [% H: o Q
you ; V; @9 }+ c$ ]) E% u) N3 \2 ?+ R% f( r" T8 Z: o
start MATLA and put the following command: X) i- ?$ k8 y R! T( c $ S4 }4 w' Q# L9 H%%%BEGIN CODE%%% 9 I2 o( D$ H4 k q2 [$ K- g% ~3 ]
maxHeapSize = 268435456 4 r5 z9 ?6 W; R4 [! X- X& m n9 E& @7 R" p# p. k! b. d2 ?2 R" ~* R
%%%END CODE%%% + Y) g' r3 p4 Q' X3 E( h' V: S8 P
This will give you 256MB of JVM memory and you can adjust the parameter3 Q4 J0 z7 _% ~; a2 J8 G
. i0 l! U) ~ B. I4 J( {/ }& e. yas needed. , z# g3 @9 S- x; ^% K6 }' n5 {; Z% |: ^
Note: $MATLAB is the root directory and $ARCH is your system% R; j8 J" ^! P, }: {( F' k% U
1 E9 U& g+ w5 K$ I9 b( D8 S& y
architecture. This solution works on Windows as well as Solaris, Linux,5 T# h& Y: b+ F7 ?9 p
: B$ y& r/ k- j- y5 |Alpha, and SGI. A similar operation is possible on IBM and HPUX, but with . k" C4 Q9 F1 V6 F! [ . n/ u0 _- o6 v( o' oa different syntax. X u6 f( g* B: j V+ |
3 A6 ]1 }, ]% H3 A. B* jFor the 1.1.8 JVM (Windows, Linux, Solaris, Alpha, SGI) our defaults are: 2 X/ y* s; x/ u+ ?9 T e8 k( F( b7 B
minHeapSize = 16000000 ; c$ `6 {0 F- ?: Y8 q/ \/ ~9 M8 F. E
maxHeapSize = 64000000 , n! l$ j2 L5 A: p" [* I) w! ~9 D- h6 M' E8 v3 h& G" s+ M0 l
These are the structure field names in that correspond to -ms and 8 ?( G7 R% p- l, w 5 M6 a0 \; ^5 W4 }, J/ b. e-mx, and the settings above are roughly 16MB and 64MB. , V* W- b6 Y( K 3 K9 A) M8 D% K: V4 E( \9 O% }3 ~, t; FTo investigate the Java heap a bit, ask via the following:( b3 i L. O3 a
+ \; j8 x- I. P. p>> java.lang.Runtime.getRuntime.totalMemory$ L: O" M6 ?7 A
. m( g8 |+ k) ^ H w" q# b>> java.lang.Runtime.getRuntime.freeMemory 8 l5 `: e0 y5 m+ x3 m7 ~9 Z : J- T+ y' R* T- D: IWhen the free memory hits zero, Java will double the heap size (up to the 4 u: A! v2 w' m* m5 G9 g4 ~9 w l8 e+ j: I/ S J% {9 k
maximum setting).# G6 W; [- p3 |1 n+ @7 ?5 J
]# h" f# {7 Z* L5 u v0 M$ _
If you choose to run without Java, you will remove the overhead of the& y H3 K6 S" ?6 m. b3 r
/ M% o6 L/ H+ K& c- ?
middle man, but you will also lose some MATLAB functionality (mostly C/ ~2 i' w& T" g8 M9 H5 \, `/ E, u# _* K% W4 D% E( d
graphics and the Editor). You will still have most of the computational 4 _& }# q: c& p5 g9 ] # `2 _3 {! G. F" Y3 jpower though. : {4 { s* f) t9 Y" d1 C# N/ C7 U2 _. K8 y: {3 I
Without J***A, memory management will come directly from the OS, and a ' C8 j3 K& M" P/ o3 A8 a. k( w: T( z% v: h
CLEAR operation will result in memory being freed back to the OS. ; `4 z- h) C( C. l+ ?4 ?+ m# D7 Y4 J& [
21)如何在Matlab中添加新的工具箱? 3 z9 }' S( s! f X8 w6 z! t! d ?" g. Q+ ~3 g
:#FangQ(Qianqian.Fang@Dartmouth.Edu),2002/6/21,BigGreen/MathTools # 7 f/ U$ E# p: `$ y2 j " }" |6 n# R! J如果是Matlab安装光盘上的工具箱,重新执行安装程序,选中即可。 0 J7 _5 B8 Y$ v6 [ d* |, h9 S6 p% j9 D, d- ~2 w
如果是单独下载的工具箱,一般情况下仅需要把新的工具箱解压到某 / ^/ A1 y( |5 ^" e- h: E% |- Q+ s . w2 K, e! f/ h个目录,然后用addpath(对于多个目录的使用genpath())或者pathtool添 / Q- i) q- X$ u1 Y) e) M( g' h F( N! g7 ^) `, S: }
加工具箱的路径,然后用which newtoolbox_command.m来检验是否可 # j, b% @4 g- ? S4 i% X. w5 X6 Q. I9 y" K6 n! @
以访问。如果能够显示新设置的路径,则表明该工具箱可以使用了。' L! {& f' Z, J1 e6 _' X
! A m, B7 ^$ @/ a! R$ Z5 V具体请看工具箱自己代的README文件。3 G2 \4 N) e( q
+ S C+ {$ x5 _: t" b4 V
22)如何读写Matlab的.mat文件? o3 _1 t7 o3 {$ e. E+ [$ ~9 X3 b
3 n2 ^- J, o, _7 L6 e! ~mxDestroyArray(T); //释放内存% r: M6 H8 W5 V
, F: o( R! o6 B# Z& i, \9 H
mxDestroyArray(mFs); 0 x& x8 k6 T& \' N, F . G1 b& p0 a1 A0 ~" h/ J0 S* EmxDestroyArray(mnfft); # `0 e- j6 ^3 H% l9 T1 {8 V ' m# U) v& D) J# PmxDestroyArray(result);' {8 X" @: f. u: @. j/ S
8 ^; ^: x$ o. jengEvalString(ep,"close;");9 F7 R- S- C& u/ E* O
# J, \/ b( n$ k$ G. O8 _) a
engClose(ep); % c6 [# _: w6 o+ X' ~, j8 y9 }0 _9 g5 f
}6 T& f+ q5 i3 u8 p
3 J n7 s0 e! J" B
上述程序在Vc下编译需要将 libeng.dll和libmx.dll两个动态库利用以下的命令:1 W( q5 ^0 k0 G" w& ^
5 J' n; p6 a8 v5 ~! Flib/def:<自己的Matlab的安装路径,下同>e:\ Matlab\extern\include\*.def /machine:ix86 /out:*.lib来生成程序所需的静态连接库libeng.lib和libmx.lib,将libeng.lib和libmx.lib所在的目录加入Vc++ project/link/object/library modules下即可。/ Y- |8 y L7 {- _: d
9 o- p1 T8 m8 K3 p6 A
利用Matlab自身的编译器调用工具箱中的函数" B1 n7 f6 a( L5 M1 E
3 n, \, B- c, O' M0 Z, s+ x
; C- f* G6 a. v% T9 y1 ?
7 x z# B- `: f& |0 x& U. \! XMatlab的编译器可将Matlab的M文件转换为为C或C++的源代码以产生完全脱离Matlab运行环境的独立的运用程序,但Matlab本身的资料说明编译器如用来建立独立的运用程序,不能调用Matlab工具箱中的函数,这非常不利于搞一些特殊的算法。本人研究了一段时间发现,工具箱中的函数既然是M文件就一定可以用编译器来编译,以提供如Vc的调用函数,但是编译器只能编译一个独立的M文件,即这个 M文件不依赖于其他的M文件。如果M文件中又调用了其他的M文件,可将被调用的M文件拷贝到调用M文件的相应位置,作适当的改动就可以用于编译器编译。编译器不支持图形函数,所以M文件中如有图形函数需注释掉。: w$ D$ b$ \5 b' W$ e |, E
e% n4 x- i9 t当Matlab的编译器mcc加入适当的参数-e(mcc –e *.*)或-p(mcc –p *.*)就可生成将输入的M文件转换为适用于特定运用的C或C++源代码。这样如果要在Vc下编译通过,还需连入以下几个库libmmfile.dll, libmatlb.dll, libmcc.dll, libmat.dll. libmx.dll. mibut.dll 以及Matlab C MATH库,建议采用前述的方法将动态连接改为静态连接。对于C/C++编译环境的设置,在Matlab command窗口下运行mex –setup 然后依提示操作,而对于C/C++连接环境的设置,运行mbuild –setup依提示操作即可。' r; f/ S. [, e
0 m7 k' Z' b' l
下面给出利用编译器将Matlab工具箱中psd.m文件生成可供Vc调用的函数。 3 A1 b2 S. t. f7 v- ~: _" R . o( n/ P, s1 S5 p将psd.m文件拷贝一份至Matlab\bin目录下,改写相应调用的M文件如nargchk.m, hanning.m等。为生成的代码简洁,对于采集数据处理输入参数很明了的情况下可作大量的删减,最终使psd.m成为一个不依赖于其他M文件的独立的M文件,注意千万注释掉作图代码,最终改成如下形式,限于篇幅给出关键的几步: # |! R- T% z3 P2 Y5 x$ `% V$ t8 D+ N. Q# q* q
function [Pxx,f]=psd(Fs,nfft,noverlap,x)4 N7 d( K: U2 A Y* Z' I" n
3 e- l6 |0 Z& _8 q( w% H3 ?; P1 o0 s
window=o,5*(1-cos(2*pi*(1:nfft)’/(nffft+1)));//hanning 窗 . Z$ L/ m5 z& |2 Y) N4 K# P# w7 ^, e; x- L/ q+ \# j$ X; m
dflag=’none’;- x$ G8 e0 P% I1 S: N
# [ I- F1 M3 G
window=window(;) 2 A/ _) F2 p" T6 F" V! l1 T+ s, F
…………………………………. 3 W( a6 [' I) J9 F " r8 V7 d9 A0 u3 H/ {以上只要稍懂Matlab语言和信号处理知识就可完成这项工作。. Y) e M3 ~" O' F
7 a+ p5 ^4 D/ w1 u! U% u8 ]( _
假设上述代码重新存为testwin.m,在Matlab command 窗口下设置好环境参数运行mcc –e testwin,则可在Matlab\bin下生成testwin.c ,如运行mcc –p testwin 则生成testwin.cpp.* C% o' K6 H3 M- D/ Y+ @; P$ l
* g1 n' K% A4 q+ V `& D
Vc下建立一个基于对话框的文件,然后在对话框里加一个Button控件OnButtonPsd 4 X% O3 p9 f4 K$ G; G# P' A4 _
将上述生成的.c文件的头文件加入到工程的.cpp中,且将#ifdef_cplusplus 7 e) V2 s2 D7 K) S " x" Y0 w4 e8 \! I# S" T' A+ xextern “c”{ ) `" {! @6 u& X. o! e! w* E7 [& u: K0 W+ r
#end if % L" m" D$ V6 s; t5 @8 s) I % ?9 Y/ C1 A. B5 V% s+ y ?( w3 r5 ic代码声明加入Vc的包含文件和生成的.C的包含文件之间 1 v `8 N8 h% u) y+ \9 a/ N: l! i0 y* w( u- p: b
将#ifdef_cplusplus & ?6 j- t7 _3 c2 E * s7 r& g3 k8 w2 v) f, |) \} 6 y/ B# B6 k/ p; T$ @8 S- r) c0 l6 g6 u
#end if加入.cpp文件未尾 1 e, o: A$ a( Z4 |4 s! T% a + ~8 n( d8 p) K7 ?! P2 v为了简洁且便于处理将生成的c函数稍改动,给出部分代码如下: 8 ^! W+ w5 @) e+ w9 Y) z8 N* }; g. n( i1 U
void CTestpsdwinDlg::OnButtonPsd(){ 8 U" [6 \- `7 }* H( X3 @ ! P9 W: L& T) L9 F: tmxArray* x_rhs_;//指向采集数据存放区& z! k6 o" Q, h: G" o3 | _
# y5 Y( V7 K; e# yFs=23510;//数据采集的频率 nfft=1024;//1024点的fft ( }; _$ D/ [& h/ C. T1 z6 i( c! H3 n4 Z T% s
double datax[1024]//采集的数据* s1 w" X9 D$ b8 T, r6 b' j
8 ^5 I8 U1 W3 M5 `/ I
x_rhs_mxCreateDoubleMatrix(1,1024,mxReal);9 }) _. `1 E! ?! ]( F! N3 X
/ s Y* d& d# q# S/ H; fmemcpy(mxGetPr(x_rhs_),datax,1024*sizeof(double));; E4 K3 Q+ J" W6 }2 f