How to draw vertical line on axes in Matlab GUI? - matlab

I have a Matlab GUI with 3 axes components. Their tags are predicted_ax, cost_ax and error_ax. I want to draw vertical line on particular position on the first axes component (the one with tag predicted_ax). How do I do that?
I tried this code:
ylim = get(handles.predicted_ax, 'ylim');
line([linePos, linePos], ylim);
But it draws the line on different axes (the ones with tag error_ax)! I'm sure I did not confuse tags or axes components. In the fact another test
ylim = get(handles.cost_ax, 'ylim');
line([linePos, linePos], ylim);
gives exactly the same result: the line is drawn on the last axes component with tag error_ax. So how do I draw the line on the right axes?

You need to set the 'parent' property of the line, as by default it will always be the current axis:
h = line([linePos, linePos], ylim);
set(h, 'parent', handles.predicted_ax);

I think you need to use the axes command to set the current axis on which the line will be drawn. Try axes(handles.predicted_ax); prior to your line command.
(Getting the ylim value for the axis apparently does not make it current.)

Related

Ticks missing in contourf

I want to plot ticks at logarithmic values (without labels, only the ticks). For some reason the following code doesn't plot them (set(gca,'XTick') doesn't do anything):
figure1=figure(1)
axes1 = axes('Parent',figure1,'YScale','log','XScale','log');
grid(axes1,'on');
hold(axes1,'on');
[C,h]=contourf(lamx(2:NKX+1),lamz(2:NKZ+1),phi2d(2:NKX+1,2:NKZ+1)',[10],'LineColor','none');
clabel(C,h);
axis tight;
axis([lamx(end) max(lamx) lamz(end) max(lamz)])
xlabel('\lambda_x','Fontsize',20);
ylab=ylabel('\lambda_z','Fontsize',20);
set(gca,'XTick',0.1:0.1:1)
grid on
set(gcf, 'PaperPositionMode','auto');
set(ylab, 'Units', 'Normalized', 'Position', [-0.1, 0.5, 0]);
set(gca,'linewidth',1.5,'FontSize',16)
colormap(flipud(gray(256)));
colorbar;
print('-dpng','-opengl','-r1200','2dspec')
How could the ticks be plotted?
The ticks in your example are obscured by the Contour object itself. The order in which the Axes elements are drawn relative to objects within it is controlled with the Axes Layer property.
The default value for this property is bottom which means anything like a filled contour drawn in the axes will be drawn on top of the ticks, and you won't be able to see them. The contourf function obviously knows this isn't a helpful default for plots that will obscure the ticks in their entirety because it usually changes the property to top when you call it – but only when the Axes NextPlot is set to replace. By calling hold on you also cause contourf to leave that property alone, so it ends up staying at bottom.
In your example you can deal with this in one of two ways.
As it's currently written, there no intentional effect of the hold(axes1,'on'); line, because you're only adding one plot. So you could just remove that line if your full code doesn't rely on it. Otherwise,
Set the Layer property to top from the outset when you create the axes:
axes1 = axes('Parent',figure1,'YScale','log','XScale','log','Layer','top');

How can I multiple plot in one figure at Matlab?

Hi I'm trying to implement as following code.
plot(bins,r);
plot(bins,g);
plot(bins,b);
But I want to plot in one figure.
Is there any way?
For multiple plots in the same figure and not the same axis. You have to use subplot(x,y,z). The first argument 'x' is the number of plot you want to produce, in your case 3. Second 'y' just adjusts the size of the plots, you can use 1. The third 'z' is the position of the plot, whether a certain plot comes first, second or third.
subplot(3,1,1)
plot(bins,r);
subplot(3,1,2)
plot(bins,g);
subplot(3,1,3)
plot(bins,g);
To distinguish between all three plot you can add another argument to plot() so that you can change colors. For example:
plot(bins,r,'r')
'r' will make the color of the plot red, 'b' makes it blue, 'k' makes it black...so on.
Yes, you can plot everything in one go:
plot(bins,r,bins,g,bins,b)
or use hold on after the first call to plot.
You need to use hold on
hold on retains plots in the current axes so that new plots added to
the axes do not delete existing plots. New plots use the next colors
and line styles based on the ColorOrder and LineStyleOrder properties
of the axes. MATLAB® adjusts axes limits, tick marks, and tick labels
to display the full range of data.
hold on
plot(bins,r)
plot(bins,g)
plot(bins,b)

Axis commands changes TightInset property to zero

I use some things from this question get-rid-of-the-white-space-around-matlab-figures-pdf-output to get rid of the white space when saving figure plots and images. This works fine, but my problem is when I use commands like "axis square" or "axis image". Ivt sets TightInset property of corresponding axes to 0 in "y" direction. I mean when I use this:
inset=get(a,'TightInset');
second and fourth numbers in "inset" are always zero, even if I set title and x-label. So in the end I don't see plot titles and x-labels.
Can I fix it somehow? So far I do it manually by adding some suitable number, but it is not convenient.
EDIT: Some more code for example. It displays two histograms.
h=figure;
subplot(121)
bar(bins,histogram1,'hist');
axis square
title('Historgram 1');
xlabel('Intensity');
ylabel('Pixel count');
set(gca,'xlim',[0 1])
subplot(122)
bar(bins,histogram_out,'hist');
axis square
title('Histogram 2');
set(gca,'xlim',[0 1])
xlabel('Intensity');
ylabel('Pixel count');
and if I call
a=get(h,'Children');
for i=1:length(a)
inset=get(a(i),'TightInset');
%...some stuff here
end
those y-related numbers in inset are zeros. If I comment axis square and do this, then inset are correct and it does what I need.
As far as I know when you use 'TightInset' you'll get only the graph or image, axis will be removed. I downloaded a function from mathworks file exchange 'saveTightfigure.m' to solve a similar problem. But I did not needed axes. If you need axes may be you can edit limits set inside the function. I mean you can give a little more space at left and bottom for keeping the axes.

How to delete the axes coordinate in Matlab GUI?

I want to make a Matlab GUI program.
When i use the axes to display the image, there are the axes number around the axes.
How to delete it?
so my GUI program will display the axes without any coordinates around the axes.
here is my code for display an image in axes.
axes(handles.axes16);
handles.image_gray = image_gray;
imshow(image_gray);
guidata(hObject, handles);
And here is the axes coordinates that i meant.
Remember that the axes is an handle object with many properties. I would recommend setting the axes properties 'xtick', and 'ytick', to an empty array. That way, you preserve the border, and background color of the axes. Simply turning the axes off will make your lines render on top of the background figure, which may or may not be the effect you are looking for.
Example:
set(handles.axes16,'xtick',[],'ytick',[])
A quick way to turn off the axis is, well, axis off:
Example
figure;
plot([-10:10],randn(21,1));
xlabel('x');
ylabel('y');
Now turn axis off:
axis off

move legend position in an empty subplot

I have a figure with 5 subplots. I declare subplot(2,3,X) which includes 6 subplots. The 6th subplot is empty. I am going to move legend to 6th empty position for all plots.
How is it possible?
if you just want to use standard-matlab, you need the handle of the subplot and then you need its position. Then you set the position of the legend to the position of the subplot.
Referring to the docs:
Note You can set the legend location by passing the 4–element position vector to the legend function using the ‘Location' option. To define the position of an existing legend, use the set function to assign the 4–element position vector to the ‘Position' property. You cannot use the Location option with the set function
for example:
subplot(2,3,1), plot(1:10,2:11)
myLegend=legend('text1')
set(myLegend,'Units', 'pixels')
myOldLegendPos=get(myLegend,'Position')
hold on
h=subplot(2,3,6)
set(h,'Units', 'pixels')
myPosition=get(h,'Position')
set(myLegend,'Position',[myPosition(1) myPosition(2) myOldLegendPos(3) myOldLegendPos(4)])
Maybe try legendflex from the File Exchange, it looks like it can do what you want.