Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB已退休,不会更新。

谁或什么获胜,ER优先?

多年来,随着我们为MATLAB构建更多功能,我们一直担心,尤其是当我们新的文件类型和数据类型时,请确保用户访问他们打算使用的工件。

Contents

We've even had commands available to help you identify what's going on, such as

Or为什么?实际上只是在开玩笑,因为最后一个在这里很有帮助。但是您可能喜欢尝试:

为什么(174)
Loren knew it was a good idea.

可变和功能优先

How does MATLAB decide, when it sees a name, whether to use a variable or which of several possible functions with the same name. MATLAB has rules of precedence, and always has. In Release R2019b, the优先规则were updated. In fact, many o f you were not affected because these changes are somewhat esoteric and are not encountered in lots of code. But when certain conditions occurred in the past, people occasionally got unexpected (from their mental model) behavior. We have tried to fix that with these updated rules. You can find the details in the release notes I noted here, or in these portions of the documentation.

您会在最后一个链接中注意到,我们提供了您之前可能写过的内容的代码示例,您看到的行为,如何使用新规则重写它以及现在应该看到的行为。您还会注意到,改变的条件围绕变量,嵌套功能,本地功能和外部功能。其中一些更改也强烈促进编写更清晰的代码,例如不命名局部变量和具有相同名称的本地功能。

班级和操作员优先

以下是其他一些其他优先规则适用的情况,尽管我注意到这些规则没有改变:

An Example

Here are 3 files for one example. We start with the code from 19a or earlier.

此功能在R2019a中没有错误运行,有2个不同的含义local.

functionmyfunc19a local(1);%本地是一个函数local = 2;disp(本地);endfunctionlocal(x) disp(x)end

R2019B中的下一个代码错误。

functionmyfuncErr19b% local is an undefined variablelocal(1);%错误local = 2;disp(本地);endfunctionlocal(x) disp(x)end

And this code works fine. And would also have run correctly earlier.

functionmyfuncGood19b localFcn(1); local = 2; disp(local);endfunctionlocalFcn(x) disp(x)end

One More Notable Change with Anonymous Functions

This is another change with respect to function behavior, specifically anonymous functions. As of R2019b, anonymous functions can include both resolved and unresolved identifiers.

Who Wins?

Hoping we all win, by having cleaner, clearer code, from which we can access the elements (variables and functions) that we are looking for. Did anyone run into issues with this change in R2019b or later? If so, please let me know这里.




与Matlab®R2020A一起出版

|