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

49 views (last 30 days)
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

Accepted Answer

James Tursa
James Tursa on 12 Aug 2023
Edited: James Tursa on 12 Aug 2023
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 Comments
Walter Roberson
Walter Roberson on 12 Aug 2023
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 on 12 Aug 2023
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.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!