Error when trying to create legend with stem plot

I receive the following error when plotting a legend with my stem plot:
??? Error using ==> feval
Value must be finite and greater than zero
Error in ==> scribe.legend.methods>build_legendinfo_component at
497
lich=feval(lic.ConstructorName,'Parent',double(p),lic.PVPairs{:});
Error in ==> scribe.legend.methods>build_legendinfo_component at
506
build_legendinfo_component(lich,gcomp(k),tx,ty);
Error in ==> scribe.legend.methods>build_legendinfo_token at 461
build_legendinfo_component(gh,gcomp(k),tx,ty);
Error in ==> scribe.legend.methods at 17
feval(args{:});
Error in ==> scribe.legend.methods>create_legend_items at 1500
methods(h,'build_legendinfo_token',tokenhandle(end),li,tokenx,tokeny);
Error in ==> scribe.legend.methods at 17
feval(args{:});
Error in ==> scribe.legend.legend at 115
methods(h,'create_legend_items',children);
Error in ==> legend>make_legend at 389
lh=scribe.legend(ha,orient,location,position,children,listen,strings,propargs{:});
Error in ==> legend at 287
[h,msg] = make_legend(ha,varargin(arg:end));
Here is the code that I use to create the legend:
stem(Tim,MetCond_RW,'MarkerSize',0,'Color','c');
stem(Tim,MetCond_RH,'MarkerSize',0,'Color','b');
stem(Tim,MetCond_SW,'MarkerSize',0,'Color','m');
stem(Tim,MetCond_SM,'MarkerSize',0,'Color','g');
stem(Tim,MetCond_SS,'MarkerSize',0,'Color','r'); hold off;
datetick('x','mm/dd');
xlim([datenum('2012/01/15 00:00:00') datenum('2012/02/05 00:00:00')]); grid on
grid on;
set(gca,'YTick',[0:1:5])
set(gca,'YTickLabel',{'N';'RW';'RH';'SW';'SM';'SS'})
%set(gca,'YTickLabel',{0; 1; 2; 3; 4; 5; 6})
legend('N','RW','RH','SW','SM','SS',2);
Here, N is 0, RW is 1, and so on and so forth. I defined these values through a MATLAB script that was called earlier. Is this because I have made the value of N to be 0? I believe that the problem persists even if I remove N. Can someone help me out here? I've spent a lot of time on this and don't really know how to proceed. Thanks for your time.

 Réponse acceptée

Is 2 intended to be a location? If so, then change it to 'Location', 'NorthWest'
It is also recommended to avoid confusion between properties and legend entries by passing the legend entries in a cell array:
legend( {'N', 'RW','RH','SW','SM','SS'}, 'Location', 'NorthWest' )

9 commentaires

Sarah
Sarah le 25 Jan 2013
Yeah, 2 was just a location. But I tried your suggestion, and I still received the same error. I can plot the stem plot without the legend just fine, but if I manually insert a legend within the plot window, I receive the same error. It definitely has something to do with the way I'm calling the legend function. 'N' has a 0 value...I wonder if that may have something to do with it?
Where is your call to create the stem for MetCond_N ?
Sarah
Sarah le 26 Jan 2013
I actually don't have a call to create a stem for MetCond_N. I just set the value to 0. Is this why I am getting the error?
How are you setting its value to 0?
If you do not have a stem() call or other graphics object corresponding to 'N', then you are going to have the wrong number of legends for graphics objects. Still, legend() would normally detect that. If, though, you use the form legend(string1, string2, ...) and legend() misinterprets one of the strings as being a property name, you might perhaps get the error you are getting. Did you switch over to using a cell array of strings for the list of strings as I showed above?
Sarah
Sarah le 26 Jan 2013
Modifié(e) : Sarah le 26 Jan 2013
I guess I use this to set its value to 0:
set(gca,'YTick',[0:1:5])
set(gca,'YTickLabel',{'N';'RW';'RH';'SW';'SM';'SS'})
Other than that, I don't call N at all. However, I just tried running this:
MetCond_N = zeros(size(Tim));
stem(Tim,MetCond_N,'MarkerSize',0);
stem(Tim,MetCond_RW,'MarkerSize',0,'Color','c');
stem(Tim,MetCond_RH,'MarkerSize',0,'Color','b');
stem(Tim,MetCond_SW,'MarkerSize',0,'Color','m');
stem(Tim,MetCond_SM,'MarkerSize',0,'Color','g');
stem(Tim,MetCond_SS,'MarkerSize',0,'Color','r'); hold off;
datetick('x','mm/dd');
xlim([datenum('2012/01/15 00:00:00') datenum('2012/02/05 00:00:00')]);
grid on;
set(gca,'YTick',[0:1:5])
set(gca,'YTickLabel',{'N';'RW';'RH';'SW';'SM';'SS'})
legend({'N','RW','RH','SW','SM','SS'},'Location','NorthWest')
title('Climatic Conditions');
I still get the same error. I tried to take N out completely (no stem call, no call as a string in legend function) but I still get the same error. This time, as you can see, I am using your cell array approach.
Sarah
Sarah le 26 Jan 2013
Let me know if it would help you to send the files over.
The problem is your MarkerSize of 0.
I'll take a look. But check this out, I run this code:
stem(Tim,MetCond_N,'MarkerSize',0); hold on;
stem(Tim,MetCond_RW,'MarkerSize',0,'Color','c');
stem(Tim,MetCond_RH,'MarkerSize',0,'Color','b');
stem(Tim,MetCond_SW,'MarkerSize',0,'Color','m');
stem(Tim,MetCond_SM,'MarkerSize',0,'Color','g');
stem(Tim,MetCond_SS,'MarkerSize',0,'Color','r'); hold off;
datetick('x','mm/dd');
xlim([datenum('2012/01/15 00:00:00') datenum('2012/02/05 00:00:00')]);
grid on;
set(gca,'YTick',[0:1:5])
set(gca,'YTickLabel',{'N','RW';'RH';'SW';'SM';'SS'})
legend({'N','RW','RH','SW','SM','SS'},'Location','NorthEast')
So I ran this, and the former error went away, but now I have this new error:
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
Any thoughts? Meanwhile, I'll investigate the markersize.
Sarah
Sarah le 26 Jan 2013
Walter, thanks so much for your help! It works....I really appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by