TA的每日心情 | 慵懒 2017-7-12 08:29 |
|---|
签到天数: 7 天 连续签到: 2 天 [LV.3]偶尔看看II 累计签到:7 天 连续签到:2 天
|
发表于 2009-8-7 23:50:24
|
显示全部楼层
本帖最后由 晓夜寒风 于 2009-8-7 23:56 编辑
: k8 @; S9 N& `; i# ~1 c3 ]" o% F! j( X1 C9 j
If the global variable does not exist the first time you issue the global statement, it is initialized to the empty matrix.
0 v9 I8 G, h; R t" \即 假设定义x 为全局变量
: ^2 F! B. ]2 C; s a( F+ v 则 使用 global x % F) B: C& J1 y' h2 u
编译的时候 解释器第一次遇到 ”global x ” 就为其分配空间,7 \+ u8 e3 i6 B. {' V
以后再从其他文件里遇到 x,就使其值与之前的x 相同。% }+ K) g- ~- V
1 |* R$ ]* S8 D& Q5 k
+ i- a7 f5 v* H! h6 Z# \ G9 e
举例* b% K2 x; h. \; q
3 J: K3 f; Y: G- z* r9 u3 B注意 文件一 与文件二 共享了 TICTOC 这个变量,在文件三中实现了时间长度计算!
6 N/ X& w2 q) K7 [3 X8 }# i3 l8 b3 l* p5 y9 s" s, b
文件一- ) x% Z4 X# j/ B3 s: A, B& q7 s0 z
- & f% z' Z9 I0 H) N
- function ticjh
& U! P: B; D/ s9 y- |7 F- @6 N& ? - % TIC Start a stopwatch timer.
^3 k" }+ Z+ n - % TIC; any stuff; TOC
8 x0 L, J/ |8 a4 V. ^ - % prints the time required.
% \9 w# j6 d$ X$ y# a- x6 g - % See also: TOC, CLOCK.
) D0 N9 E6 b6 m5 Z# |9 N, q7 L! O- [ - global TICTOC
3 w; [4 O) } A5 g e1 w" E1 t - TICTOC = clock;
' j: C c2 m0 r
复制代码 文件二
( j( u) f# V& V9 L# f! G4 X- function t = tocjh2 _4 u6 i- A' [$ P9 |
- % TOC Read the stopwatch timer.4 D: a. ^+ { R1 y0 W
- % TOC prints the elapsed time since TIC was used.
( ~4 G, g: B. n. [: o - % t = TOC; saves elapsed time in t, does not print.' d) C, H, j# _+ Q" a+ t8 P7 w
- % See also: TIC, ETIME.
) Y l: \+ W- k, Z( d, ?- Q - global TICTOC
0 N k$ y8 F( s# ]) z" u - if nargout < 1) K! E, H7 |" [$ }" C: M
- elapsed_time = etime(clock, TICTOC)
6 U8 O0 o& ?. D4 { - else% G* D8 I& F, h2 f' N6 @9 _8 n
- t = etime(clock, TICTOC);
; o# @ M* y6 g$ n6 O/ b/ ] - end
) x3 W% b; G6 Z7 S# _: G
复制代码 文件三
: H- M' k# v1 Z6 r- ticjh;4 v9 K2 W2 N9 I
- for i=1:1:100
8 i; T4 n6 B. w8 W+ Z# y% C - for j=1:1:1002 q V- k. |3 |$ m
-
, i( Y {& G, I( T' V5 Q - end7 }/ J1 [. Z2 c
- end
6 ?+ A/ K6 ?( c9 G - t=tocjh;
3 i8 L b4 d. N( Q
复制代码 PSAT 中应用了很多global。 |
|