TA的每日心情 | 慵懒 2017-7-12 08:29 |
---|
签到天数: 7 天 连续签到: 2 天 [LV.3]偶尔看看II 累计签到:7 天 连续签到:2 天
|
发表于 2009-8-7 23:50:24
|
显示全部楼层
本帖最后由 晓夜寒风 于 2009-8-7 23:56 编辑 9 V" _: K \; U2 p
; N1 Q& Q9 ], H" w9 b: E
If the global variable does not exist the first time you issue the global statement, it is initialized to the empty matrix.* @; \. |0 e' t$ T5 K U
即 假设定义x 为全局变量3 \$ l8 [6 K' h' O: x0 R4 X
则 使用 global x 7 ]0 Z# P" Z- |3 j1 H
编译的时候 解释器第一次遇到 ”global x ” 就为其分配空间,
6 T5 Z+ w$ j5 f5 D4 B. q, X0 C 以后再从其他文件里遇到 x,就使其值与之前的x 相同。
% U8 `9 O b& N, M4 q, s z$ \) B- `' u! m- x5 v/ u
- }4 w% K+ o( s4 H9 z& R举例1 q m3 d& M8 d0 V7 ?
8 c4 D# P5 u8 D$ A注意 文件一 与文件二 共享了 TICTOC 这个变量,在文件三中实现了时间长度计算!
7 z; Y2 x. _. n9 }; a2 C. G9 @- c* K1 w5 V* T$ j0 t
文件一
) o2 e8 |# M1 i6 ]! w- 5 y7 u) U+ n- z2 ^8 d" u
- function ticjh( w& o, h* x3 p* |# H! v7 S% c. N
- % TIC Start a stopwatch timer.: Y2 O8 Z8 @$ z* T$ A
- % TIC; any stuff; TOC
* a" `! f k( `$ o' y9 c+ w - % prints the time required.
4 a5 x1 t# t( O, L, P9 c- g @: a! A - % See also: TOC, CLOCK.2 J8 q- V7 L _; M
- global TICTOC
% h! q; k/ d! t/ L" i - TICTOC = clock;
& B2 k' @5 f6 ^5 n# J3 Q/ _2 R% h
复制代码 文件二- 9 U4 F1 [, o u% V% `( Z; ~# k
- function t = tocjh! F: O; G% r2 H4 g2 k& u& g
- % TOC Read the stopwatch timer.
% g; e: ]( g$ m6 Q - % TOC prints the elapsed time since TIC was used.
5 ^' H3 D" }0 \- |$ ? - % t = TOC; saves elapsed time in t, does not print.6 c4 I8 L8 P. |- H
- % See also: TIC, ETIME.
" |5 g. o$ L- M/ B: I7 R1 E - global TICTOC! u+ R) s2 u9 e! x& D% d, g& D
- if nargout < 1
7 c4 Z% H& f/ t# V7 [' b$ T- D - elapsed_time = etime(clock, TICTOC); v5 G" [9 h' I6 h, r! w
- else
; b6 H" Q7 ]3 ~* g) I5 w - t = etime(clock, TICTOC);8 @) s( ^6 y e5 x3 _0 O
- end
! |0 `, j1 U. O& \8 V
复制代码 文件三- - J3 i5 X# y3 r
- ticjh;# w2 {/ |2 j0 G7 P
- for i=1:1:1007 {9 n. ]' |+ Q; }: @; O
- for j=1:1:1001 g* H6 @: d9 o2 Z
-
8 G# @( U$ h' E - end
" N! \: @2 O* \: o - end, B' ]2 L0 D& ] H" d
- t=tocjh;! v% ?7 A& Q3 }' K( s
复制代码 PSAT 中应用了很多global。 |
|