Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在matlab中更新变量?_Matlab_Variables_Hyperlink_Refresh - Fatal编程技术网

如何在matlab中更新变量?

如何在matlab中更新变量?,matlab,variables,hyperlink,refresh,Matlab,Variables,Hyperlink,Refresh,有没有办法更新一个变量的值,这取决于另一个变量 看看这个简单的例子: 代码: variable1 = 1; variable2 = 2 * variable1 variable1 = 3; variable2 variable2 = 2 variable2 = 2 variable1 = 1; variable2 = 2 * variable1 variable1 = 3; variable2 = 2 * variable1 variable2 =

有没有办法更新一个变量的值,这取决于另一个变量

看看这个简单的例子:

代码:

variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2
variable2 =

     2


variable2 =

     2
variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2 = 2 * variable1
variable2 =

     2

variable2 =

     6
输出:

variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2
variable2 =

     2


variable2 =

     2
variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2 = 2 * variable1
variable2 =

     2

variable2 =

     6
我希望,在更改variable1的值后,variable2将更改,而无需像这样重新定义:

代码:

variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2
variable2 =

     2


variable2 =

     2
variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2 = 2 * variable1
variable2 =

     2

variable2 =

     6
输出:

variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2
variable2 =

     2


variable2 =

     2
variable1 = 1;

variable2 = 2 * variable1

variable1 = 3;

variable2 = 2 * variable1
variable2 =

     2

variable2 =

     6

据我所知,这不能在MATLAB中自动完成

我将创建一个脚本来执行所有更新,并在每次更改
variable1
时调用它

variable1 = 1;
update_vars

这是我能想到的最好的办法。它易于编写,易于阅读。

为什么不想使用函数

function [Out1, Out2] =  setVariables(Arg1);
   Out1 = Arg1;
   Out2 = 2*Arg1;
end


[Variable1, Variable2] = setVariables(2)
或者使用syms(当然,这也可以通过函数实现)


您可以使用eval功能:

variable2 = eval('2*eval(''variable1'')')

如果您确实想(不应该),可以将
variable2
等设置为函数,并使用
evalin
获取
variable1
的当前值:

variable2 = @() 2 * evalin('caller', 'variable1')
variable1 = 1;
variable2() % output: 2
variable1 = 15;
variable2() % output: 30
这是一个相当丑陋的解决方案,我自己也不会使用它。如果您的代码位于函数中,则有一个稍微好一点的解决方案(在不使用eval的意义上):

尽管如此,我个人还是会使用类似于Robert p的
update\u vars
脚本的东西。同样,如果您在函数中,您可以使用这样一个事实,即可以访问和修改其父变量中的所有变量:

function [ ... ] = myFun( ... )
variable1 = 1;
updateVars();
variable2 % 2
variable1 = 15;
updateVars();
variable2 % 30

    function updateVars
        variable2 = 2 * variable1;
    end
end
正如其中一条评论中提到的(不太具体),通过创建一个具有“动态”计算的依赖属性的类,可以实现这种行为。例如:

classdef ReplaceWithAMeaningfulName < handle
    properties (Dependent)
        data % The property that holds the instance's main value
    end

    properties (Dependent, SetAccess = private) % Only get-access, please
        someCalculatedValue % Something calculated
    end

    properties (Access = private)
        m_data % The private storage for data property
    end

    methods
        function self = ReplaceWithAMeaningfulName(data)
            % Constructor
            if nargin > 0
                self.data = data;
            end
        end

        % Property accessors for data property
        function set.data(self, value)
            assert(isnumeric(value), [mfilename ':InvalidValue'], ...
                'Value for property ''data'' must be numeric.');
            self.m_data = value;
        end

        function value = get.data(self)
            value = self.m_data;
        end

        % Property accessor for your calculated value
        function value = get.someCalculatedValue(self)
            value = self.data * 2;
        end
    end
end

通过将
variable2
从常规变量更改为嵌套函数,可以非常接近。在Matlab中,变量和函数调用具有相同的语法(“统一访问原则”的变体),因此可以直接进行替换。这在您的案例中是有效的,因为Matlab中的嵌套函数是一个“闭包”,因为它可以访问封闭工作区中所有变量的实时更新值

这只适用于函数内部,而不适用于脚本或命令行

function foo

v1 = 1;

% Define v2 in terms of the "live" v1 value
function v2_val = v2()
v2_val = 2 * v1;
end

v2  % evaluates to 1
v1 = 3;
v2  % evaluates to 6

end

这根本不是matlab或任何其他语言中变量的工作方式。有些语言支持按您的要求计算的属性,但我不知道matlab是否如此。最好的选择是使用指针,不幸的是,matlab不支持。指针如何解决这个问题?它们对于将一个变量精确地别名到另一个变量非常有用,但不会将变量的内容作为另一个值的函数重新计算;它将具有与
variable2=2*variable1
完全相同的行为。