Simple legend function no longer works in R2014B

I just updated to R2014b. I have a simple line as follows in many of my plotting scripts, and none of them works anymore.
For example, if I plot two curves and want to label one as 'A' and one as 'B', I use the following line after the plots:
legend('A', 'B')
This used to work, but after updating Matlab, I get the following error in every script:
Error using legend (line XXX)
Too many input arguments.
Has anyone else had this problem? What do I have to change?
Thanks.

 Accepted Answer

Sounds like a shadowed file. What is the full text of the error message?
Run the following:
dbstop if error
This will stop on the offending line when the error occurs. Once on this line, run:
which -all whatever_functions_are_there
On all of the functions. More than likely one of them is yours that has been accidentally added to the path.

5 Comments

Sean,
Thanks for trying to help me.
Here is more complete information.
Here is the script. The only function call is to legend.
% SimplePlot.m
A = 1:5;
B = 2:6;
figure(1)
plot(A, '-r', 'LineWidth', 2)
hold on
plot(B, '-b', 'LineWidth', 2)
hold off
legend('A', 'B')
grid on
xlim([0 6])
ylim([0 8])
xlabel('X')
ylabel('Y')
title('Plots of A and B')
Here is the error message I get when I run the above script:
>> SimplePlot
Error using legend (line 120)
Too many input arguments.
Error in SimplePlot (line 9)
legend('A', 'B')
If I go to the function legend.m, I see the following around line 120, which is the “throw(me)” line:
% First we check whether Handle Graphics uses MATLAB classes
if ishg2parent( varargin{:} )
% Legend no longer supports more than one output argument
% Warn the user and ignore additional output arguments.
try
[varargout{1:nargout}] = legendHGUsingMATLABClasses(varargin{:});
catch me
throw(me)
end
Here is what I get when I do what you suggested (not sure I understood exactly what I should type, though):
>> dbstop if error
>> SimplePlot
Error using legend (line 120)
Too many input arguments.
Error in SimplePlot (line 9)
legend('A', 'B')
120 throw(me)
K>> which -all legend
C:\Program Files\MATLAB\R2014b\toolbox\matlab\scribe\legend.m
K>>
When my sysadmin installed R2014B, he did not uninstall R2012B. I don't know if that could cause the "shadow file" problem you mentioned.
Sue
You script works perfectly for me, legend, labels, title, colours, and everything.
Run:
which legend -all
from the Command Window.
When I do it, I get:
C:\Program Files\MATLAB\R2014b\toolbox\matlab\scribe\legend.m
If you get something different, post it here.
It probably is a shadowed file issue, but Tech Support has better resources for debugging it, i.e. WebEx, and experience with this type of error. Give them a call! It's free if your company/university is current on SMS.
I get the same thing you do when I type "which legend -all" so I'll call Tech Support. Thanks for your help.
Sue
Good luck!
Post the solution here as well when you get this problem resolved.

Sign in to comment.

More Answers (2)

I believe Sean was correct about the shadowed file. I contacted Tech Support, and they also believed that to be the problem. They told me the following:
*"The typical cause of such an issue is the original function being shadowed by another function with the same name. However, based on the discussion on your post and the tests such as executing "which -all legend" that you conducted, it seems that function shadowing is not the reason.
Now, the implementation of "legend" has changed significantly as of MATLAB R2014b. Also, in a comment on your post, you mentioned that R2012b was not uninstalled. In light of this, one possible reason for this issue to occur might be that the path has not been set correctly for the new installation and parts of the path might still point towards the directory location where R2012b is installed. To see if this is the issue, please execute the following commands:
1) Restore the path and rehash the toolbox cache using the following commands
>> restoredefaultpath
>> rehash toolboxcache
This will make sure that MATLAB search path is set correctly for the R2014b release. Please note that executing these two commands will reset the search path and any custom functions or scripts that you have added to the search path manually during your previous use will have to be added to the path again.
2) After you execute these commands, test your "SimplePlot.m" script again and see if it works. If the script executes as expected and does not throw an error, execute the following command at the MATLAB command prompt.
>> savepath
If the issue is not resolved, close and reopen the MATLAB session and then try the following steps:
1) Find the MATLAB preferences directory location by executing
>> prefdir
2) Open Windows Explorer and navigate to the folder location output by "prefdir". Rename the folder to something else, and then restart MATLAB. For example, the output of "prefdir" would look something like:
C:\Users\......\MATLAB\R2014b
Rename the folder "R2014b" to something else like "myR2014b". Next close the existing session of MATLAB and start a new one.
This process will regenerate the preferences directory that stores the state of your latest MATLAB session."
So I tried all of the above, and then re-ran SimplePlot.m. I still got the error. So I decided to limit my search path to just that script and no other. Then everything worked fine.
I added back into the search path all of my Matlab folders, one by one, and re-ran SimplePlot.m each time. It ran just fine until I added in an old set of scripts that I got from the File Exchange - Electromagnetic Waves & Antennas (EWA) Toolbox by Sophocles Orfanidis. (He has since posted newer scripts that don't cause any problems.)
Once I got rid of the older EWA scripts, the problem has not recurred.
Thanks for everyone's help, including Aniruddha Katre at Tech Support.
Sue
thank you! I come across the same question with you and pick up your help!

Tags

Community Treasure Hunt

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

Start Hunting!