How do you fix the size of an axes graphic object? - matlab

I'm creating a simple GUI in MATLAB and I'm trying to create a figure that contains a 2d plot on an axes, all is working well, but I have a rough time trying to figure out how to fix the position of the axes/plot.
The axes seems to scale with the figure window size. If I maximize the figure on my screen, the axes is maximized within, etc. What I want to accomplish is to have a row of buttons beneath the plot on the same figure as the plot, basically an area of 50 (ish) pixels tall that the plot does not encroach on. I know how to do this in HTML, but I can't figure out a good way in MATLAB.
Any alternative approaches would also be greatly appreciated.

Change the units of the axes to anything other than 'normalized', like 'pixels'. Then it won't automatically resize with the figure. From this documentation page:
When you create a graph, MATLAB® creates an axes to display the graph. The axes is sized to fit in the figure and automatically resizes as you resize the figure. MATLAB applies the automatic resize behavior only when the axes Units property is set to normalized (the default).
Use set(gca,'Position',pos) where pos = [x y w h] to set the position and size of the axes in the units you chose.
See this answer for an example and a function for holding an axis size once you have it in place.

Related

Adjusting window-size to surf()-plot, Matlab

We're encountering a problem while plotting the frequency of our signal with the surf()-function. As you can see in the picture below, it doesn't fully fill up the plot window. How do we adjust it so that the left plot fills out the graph as well as the right one (which is using spectrogram())?
Picture of our plots:
You can use xlim and ylim to set the limits of the x and y axes. In your case, you could consider something like:
ylim([min(Frequency) max(Frequency)]

Default display of axes in Matlab Gui

I tried using axes for displaying images in Gui.But, before displaying any images, the axes is shown with a plot figure while running the GUI, something like below.
You can see the default axes being displayed. Is there a way to display the axes in running GUI without displaying these plot figures? So that when the image is not displayed in the axes, nothing is displayed. Thanks in advance.
UPDATE 1
I have used 9 axes here, thus the long trail of y axis.
Yes, you can use
axis off
To remove the axes from the empty plots. Then use
axis on
When you actually plot something to bring them back.
Best,

Place MATLAB legend such that it does not overlap on the plot

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')

Matlab: Put an image ON TOP of a plot, after the data has been plotted

I have searched the internet for a solution to the question above but have had no luck up to now. I have been producing a number of 2D plots where the origin of (0,0 point) is represented by an image. I have made these by plotting the data on an image, where the image is all white apart from the desired symbol at the center point (so I can reshape it as needed). I then move the axis so they cross at the center point. This all works fine, but I now have to create a number of plots using ‘fill’ to place two shaded areas on the plot that overlap in the center. This causes the symbol to be difficult to see even using ‘alpha’.
I therefore have two options to get the desired effect, both requiring me to put an image on top of the figure after the data is plotted. These options are:
1) I place the image on top of the plot and apply alpha to it (May not look very good as it will mute the plot).
2) The better option would be to crop the image around the symbol and then position it on top of the plot so the image center is at the origin (I have no idea how to position the image this way).
Both methods need the image to be placed on top of the plot after the data is plotted. I have tried 'hold on' and calling 'figure(imagesc(Image))' neither work. I have Matlab 2012b but no toolboxes (so cannot use subimage etc.)
Thanks for any help you can give
You can set the transparency of individual pixels of an image using the 'AlphaData' option. So, you can plot the overlay as follows:
% plot your data normally
...
hold on
% assuming the overlay is a 11x11 image
image(-5:5,-5:5,image_matrix,'AlphaData',alpha_matrix);
image_matrix would obviously be the matrix with your image data stored in it, while alpha_matrix would be a matrix of the same size as image_matrix. Every pixel you want to show would have a value of 1, every pixel you want to hide (the white pixels) would be 0.

MATLAB Graphing plots with axes

I have been trying to Create a program with the GUI in MATLAB. When I try to plot information with AXES I can not figure out how to do it. I know about the function plot, but I need to be able to re-size and move the plot around in the figure so I can make room for the input uicontrol. I am not sure what to do. Please help.
There are a couple of different ways to tackle this problem. But the way I always choose to do so, is:
hold on:
plot(...), xlim([xmin xmax), ylim(ymin ymax])
This should set your bounds on the axis.
I don't have Matlab at hand right now, but try the following:
To set size of the plot axes inside the figure window use
set(gca,'Position',[left bottom width height])
see Mathworks' site on axes properties