GLOBAL 与全局关键字

GLOBAL vs global keyword

我正在研究我在其中发现的 MATLAB 代码:

GLOBAL = eye(4,4);

什么是GLOBAL?它是一个全局变量吗?文档说全局变量是这样声明的:

global x1 = 4;

GLOBALglobal有什么区别?

GLOBAL = eye(4,4); 确实使 GLOBAL 成为一个变量。这还不错,因为 MATLAB 区分大小写,当然不是很清楚。您找到的记录版本是 global in lower case. I'd suggest not naming variables the same as built-in functions, e.g. if you calculate a sum don't call it sum but Summed or something; don't call an average mean,但 Avg

我写了一个突出区别的小脚本:

clear all;
global x1;
x1 = 4;
GLOBAL = eye(4,4);
whos

正如您在工作区中看到的那样,x1 是一个全局变量,而 GLOBAL 不是:

 Name        Size            Bytes  Class     Attributes

  GLOBAL      4x4               128  double              
  x1          1x1                 8  double    global    

编辑:您甚至可以全局声明 GLOBALglobal GLOBAL 然后导致:

  Name        Size            Bytes  Class     Attributes

  GLOBAL      4x4               128  double    global    
  x1          1x1                 8  double    global