Unusal error "Current use of the variable is inconsistent with its previous use or definition".

52 次查看(过去 30 天)
Hi,
I recently updated to Matlab version R2021b and noticed an unusal error. Suppose, I delacre two variables 'a' and 'b' and when I add them together, I get an error "Unrecognized variable 'a' ". But If I write the same equation differently by adding or removing some spaces, it works fine (see the code snippet below). When using in a script, I get a error "the current use of 'a' is inconsistent with its previous use or definition".
Am I missing something obvious? I don't remember seeing this issues in the previous versions of Matlabs. Any clarification is appreciated.
a = 10;
b = 10;
a +b %shows error Unrecognized variable
Unrecognized function or variable 'a'.
a+b % this is fine
a+ b % this is fine
a + b % this is fine
a + b % this is fine
c = a +b % this is fine

采纳的回答

James Tursa
James Tursa 2023-8-12
编辑:James Tursa 2023-8-12
See how the +b is purple? That means MATLAB thinks you are invoking a as a function with +b as a character string input. Hence the error since a is not a function. This form of calling functions with character string inputs has been around for a long, long time. E.g., here is an example of using this form of function calling with a different function that takes character string inputs (see how the ThisIsAString is highlighted purple in both cases, meaning MATLAB is treating it as a character string):
disp ThisIsAString
ThisIsAString
Does the same thing as
disp('ThisIsAString')
ThisIsAString
Spacing matters with your case, so use one of the other forms that works.
  8 个评论
Walter Roberson
Walter Roberson 2023-8-12
Does python have a "command" syntax (that is not down to restricted keywords) ?
There is a situation in MATLAB in which a + in that context would not be a clue that mathematics was intended.
MATLAB has a "which" function / command which is most often used in command form, such as
which classify
but it is expected to also be able to handle lines such as
which +java/+lang/serialport
which sym/beta
which double>empty
and of course there are command syntaxes that are expected to work such as
ls C:\Users\该帽子\Desktop
Steven Lord
Steven Lord 2023-8-12
There are some functions in Cleve Moler's original MATLAB that can be called in command form. The Appendix section of this blog post says that you can use the commands "help \ , help eig" to get some help. The help for the CLEAR function also says "CLEAR X" works. So this has existed for at least 40 years.
In fact you've almost certainly used command form without realizing it, for one or more of the following six functions.
  • help
  • doc
  • close
  • clear
  • hold
  • edit
If you've ever written help plot, doc ode45, close all, clear all, hold on, or edit myfun you've used command form. You could have called those functions using function form as help('plot'), doc('ode45'), close('all'), clear('all'), hold('on'), or edit('myfun') with only a few more keystrokes.
The rules for what counts as command form and what's interpreted as an operator call can get complicated.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by