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

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

问题描述

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



请不要回应:

 全球xyz; 

因为我也可以阅读帮助文件



我声明了一个全局变量<$ c

$ p $ function [x] = test($ c $ x $)$ ,然后做了这样的事情:


全球x;
test1();
end

函数 test1()

  function test1()
x = 5;
end

当我运行 test(),我的输出是 x = [] 。有没有办法让它输出 x = 5 ,或者是我定义 x 的任何东西,功能?在C中,这将是一个外部变量,我认为使它成为一个全局变量应该能够实现这一点。 您需要宣布 x 全局变量在每个范围内(即功能/工作区),你希望它被共享。因此,您需要将 test1 写为:
$ b

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


Is there a way to declare global variables in MATLAB?

Please don't respond with:

global x y z;

Because I can also read the help files.

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

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

Where the function test1() is defined as:

function test1()
    x = 5;
end

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.

解决方案

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天全站免登陆