MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中声明全局变量 (https://www.labfans.com/bbs/showthread.php?t=26290)

poster 2019-12-14 20:13

在MATLAB中声明全局变量
 
有没有一种方法可以在MATLAB中声明全局变量?

请不要回复:

global xyz; 因为我也可以阅读[URL="http://www.mathworks.se/help/matlab/ref/global.html"]帮助文件[/URL] 。

我已经声明了全局变量x ,然后执行了以下操作:

function[x] = test() global x; test1(); end 函数test1()定义为:

function test1() x = 5; end 当我运行test() ,我的输出是x = [] 。有没有办法让我输出x=5 ,或者我定义x在单独函数中的任何内容?在C语言中,这将是一个外部变量,我认为使其成为全局变量应该可以实现这一目标。



[B]回答:[/B]

您需要在希望共享的每个范围(即函数/工作空间)中将x声明为[URL="http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html#f0-38470"]全局变量[/URL] 。因此,您需要将test1编写为:

function test1() global x; x = 5; end

[url=https://stackoverflow.com/questions/4911926]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 04:55

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.