Why do I get an error in a program that used to work properly and/or still does on other installations of MATLAB?

3 views (last 30 days)
I have a MATLAB program that recently stopped working properly. I get different error messages such as one of the following:
??? Subscript indices must either be real positive integers or logicals.
??? Index exceeds matrix dimensions.
??? Error using ==> functionname
Too many input arguments.
??? Error using ==> functionname
Too many output arguments.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
In MATLAB you have the possibility to overwrite or overload built-in functions or operators in different ways. For example you could overwrite the function PLOT if you create a file called plot.m in your work folder and write some code in it. From this point on, whenever you or another function tries to use the function PLOT, it will use the file in your work folder instead of the built-in function.
You can use the function WHICH to determine which function will be called when you execute it:
which plot
built-in (C:\MATLAB\R2008b\toolbox\matlab\graph2d\plot)
The following example demonstrates what happens when you overwrite the EXP operator by using it as a variable:
Use EXP.
exp(2)
ans =
7.3891
Determine what is called, when EXP is executed.
which exp
built-in (C:\MATLAB\R2008b\toolbox\matlab\elfun\@double\exp) % double method
Overwrite operator.
exp = 345
exp =
345
Try to use EXP again.
exp(2)
??? Index exceeds matrix dimensions.
Determine what is called, when EXP is executed.
which exp
exp is a variable.
Restore operator by deleting the variable. Confirm that EXP works properly again.
clear exp
exp(2)
ans =
7.3891
If you get one of the above error messages, or any similar messages that could point to built-in functionality being overwritten, then debug your program and use the function WHICH to determine if the function being called is the correct one.
This problem can also affect Simulink and Stateflow, as they also use MATLAB functionality underneath.
Here are some best practice suggestions to avoid this type of problems:
1. MATLAB functions are always written in lower case. Avoid creating variables with short names in lower case, as these might overwrite built-in functions. For example instead of using a variable called 'exp', use one called 'myExp' or 'tempExp'. By following this convention of concatenating at least two words using mixed case for variable names, you will minimize the risk of creating conflicting variables.
2. If you are unsure if your variable or function name will conflict with existing functionality, use the function WHICH in the following ways to determine any potential problems:
which myfunction
which myfunction –all % Shows all occurrences of myfunction in the MATLAB path.
3. Often the problem might not be that you directly overwrote a function such as PRINT, but you could also have overwritten a function that PRINT itself calls when it gets executed. For example you or a third-party toolbox (mostly an old version) that you have installed could overwrite a keyword such as FOR, GLOBAL, etc. which will render PRINT and many other functions useless.
It is not possible for MATLAB to warn about functions being overloaded/overwritten as the whole MATLAB architecture relies heavily on this feature and it would thus cause warnings to be shown unnecessarily.
More information about the purpose and value of this MATLAB capability can be found in the documentation:
%Overloaded Functions
web([docroot '/techdoc/matlab_prog/f4-73527.html'])
%Overloading MATLAB Functions
web([docroot '/techdoc/matlab_oop/brdqi79.html#brdqi89-1'])
%Overloaded MATLAB Functions
web([docroot '/techdoc/matlab_prog/f0-50163.html#f0-50179'])
%Overloading Operators
web([docroot '/techdoc/matlab_oop/f1-6624.html#f1-6626'])

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2008b

Community Treasure Hunt

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

Start Hunting!