필터 지우기
필터 지우기

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

조회 수: 32 (최근 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