Matlab 2016a legend line width - matlab

I want to plot the legend in a Matlab figure using R2016a. Sample paint image left is what I get right is what I want:
But the lines inside the legend box are too narrow. How can i make them longer?
Note that I am using Matlab 2016a. I cant get the legend properties and change them with code since when I type:
lgh=legend;
I get that lgh is a matlab.graphics.illustrator. Legend and when I double click it I go to the property inspector where I cant change the width of the lines inside the legend.

You do not have to create an object, you could simply type in >> legend on
on the command window, and legend will be added on the active plot.

Related

Colorbar properties

I am trying to plot a contour with a colorbar that has a small values (10e-9). This value appears at top of the colorbar. How can I change the location of this value from top to bottom of the colorbar. I want to move this value beneath the colorbar because when I added label above the colorbar it overlapped with this value
I attached image for the figure.
You can accomplish what you want by first finding the handle to the color bar, then changing the yticklabel ticklabels property. This is a cell array of stings, one for each tick mark. You can fill in whatever you want to show there. The multiplier at the top will go away. With the text function you can add your own modifier anywhere you want. But I think it looks nicer within the axis label.
However, the simple solution is to change the units you plot. Multiply everything by 10e9 before plotting, then add a nano prefix to your units.

How to define Matlab Bar3 DisplayName for individual surface objects

I have a series of histograms being plotted along side each other in a bar3 plot. I'd like to take advantage of the plot browser to turn on and off various histograms so I can do side by side comparisons. You can see from the properties Inspector that I've altered the display name for one such surface, the third, that is being updated in the legend but not in the property browser.
There's also a misregistration of the colors you see in the legend to that of the actual plot. The legend is accurate only when I have all surfaces checked for display.
I'm using MATLAB Version 7.13.0.564 (R2011b)
Thanks for helping
Toggle the legend off and on with legend('off') followed by legend('show'). Try also legend('toggle').

Fix position of legend in MATLAB

How do I fix the position of a legend in a MATLAB figure?
I'm currently building a GUI, intended for public use, and when figures inside the GUI are produced I do not want the user to be able to move the legend by click-and-drag. Anyone?
You have to remove the buttonDownFcn from the legend.
snippet:
line(rand(1,3),rand(1,3))
l=legend('location','ne')
set(l,'ButtonDownFcn',[])

Remove Objects from Legend When You Have Also Used Fit, Matlab

I've plotted data points and fitted an exponential curve between them using 'fit' in Matlab. The problem is that with the fit-function I got the fitted line I wanted to plot, but also extra markers on top of my regular markers. I solved this problem by plotting the wanted markers on top of the unwanted so that they couldn't be seen. Now to the problem. When I want to show the legends those dots are also in there. How can I remove the markers from the legend without removing the fitted line since both of them are hidden inside the fit-function? Can I stop 'fit' from plotting the unwanted markers? So, I want to remove the blue dot called 'hoff' in the picture below.
You can leave out legend-entries by manually leaving out the handles of the lines, that you dont want to be in the legend. Try this:
%if you plot something, that you want showing up in the legend, save its handle:
h_line = plot(x,y)
%you dont want to show up this line? dont do anything, just plot it:
plot(myMarker)
%then set the legend-> you can add the text for your legend-entries with
%a cell-array containing the strings you want to show up:
legend([h_line another_line],{'Text1' 'Text2'})
with the example (see comments) I came to this solution:
close all
X=[1:10];
Y=X*0.5+0.1;
ft = fittype('poly2');
f = fit(X', Y',ft);
ha=plot(f)
hold on
hc=plot(X,Y)
hb=errorbar(X, Y, X*0.1, 'squarek','MarkerFaceColor','k','markersize',5)
hleg1 = legend([ha hc],{'hnh','ghg'});
-> this is just about splitting the plot-command. Hope that helps...
the result should look like this:

How to add a x-axis line to a figure? (matlab)

I want to add a x-axis line at 0 to a Matlab figure so that I can compare my data to see if it is positive or negative when saving the figures to a jpg. What is the best way to do this? I know you can use line() but it just seems cumbersome because you need to specify the x and the y ranges. Is there an easier way?
There exist an undocumented function graph2d.constantline:
plot(-2:5, (-2:5).^2-1)
%# vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');
%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');
The nice thing is that it internally implements a listener for the axes limits (handles change like pan, zoom, etc..). So the lines would appear to extend to infinity.
You could get this x range directly after the figure has been created. It goes a little something like this:
x=-2:5;
y=x.^2-1;
figure()
plot(x,y);
xlim = get(gca,'xlim'); %Get x range
hold on
plot([xlim(1) xlim(2)],[0 0],'k')
Note that if you do any manual zooming out in the figure, the line might have to be redrawn to go over the entire new x range.
I don't believe there is a built-in way that is more convenient. I use hline() and vline() from FileExchange, which work like a charm:
http://www.mathworks.com/matlabcentral/fileexchange/1039
A vline and hline command like in GNU R would be great, but I could not find a shorter solution than
plot(1:10,sin(1:10));
line(xlim,[0 0],'Color','r')
Draw your data by plot() command or stem(). A figure window will open.
Then on the figure window, click on the [insert] command from the
menu bar, a drop-down menu will appear.
From this menu click on the [line] command, now the shape of the
cursor will change into a plus sign.
Now you can draw a line anywhere you want, either horizontal or
vertical or slanted.
You can change the properties of line by right clicking on the
line, a menu will appear from which you can choose your desires
properties.
If you want to have some ticks on the line then you can use add
text option, and place text where ever you want.
If you would like to have a code for your figure, click on [file]
menu and then click on [generatecode] option, a new text editor
window will open, you can save this code for further use. Good luck.
Since MATLAB R2018b there is yline for this purpose:
yline(0)
draws a horizontal line at y==0.