在 MATLAB 中声明一个全局变量 [英] Declaring a global variable in MATLAB

查看:45
本文介绍了在 MATLAB 中声明一个全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 MATLAB 中声明全局变量?

Is there a way to declare global variables in MATLAB?

请不要回复:

global x y z;

因为我还可以阅读帮助文件.

我已经声明了一个全局变量 x,然后做了这样的事情:

I've declared a global variable, x, and then done something like this:

function[x] = test()
    global x;
    test1();
end

其中函数 test1() 定义为:

function test1()
    x = 5;
end

当我运行 test() 时,我的输出是 x = [].有没有办法让它输出 x=5,或者我定义的任何 x 在一个单独的函数中?在 C 中,这将是一个外部变量,我认为将其设为全局变量应该可以实现这一点.

When I run test(), my output is x = []. Is there a way I can make it output the x=5, or whatever I define x to be in a separate function? In C, this would be an external variable, and I thought making it a global variable should accomplish just that.

推荐答案

您需要将 x 声明为 全局变量 在您希望它被共享的每个范围(即函数/工作区)中.所以,你需要把 test1 写成:

You need to declare x as a global variable in every scope (i.e. function/workspace) that you want it to be shared across. So, you need to write test1 as:

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

这篇关于在 MATLAB 中声明一个全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆