How to auto adjust an Axes to fit a 3D plot in Matlab GUI
17 views (last 30 days)
Show older comments
Randima Hettiarachchi on 13 May 2014
Commented: Randima Hettiarachchi on 22 May 2014
I have an axes object in my GUI to which I'm filling a 3D plot during run time.
However, this 3D plot is not properly displayed within the axes and I suspect it happens due to predetermined size of the axes object I put in my GUI (please see the code and the result I get below).
The same plot is displayed properly when I plot it in a new figure with the "figure" command (please see the code and the result below).
I tried all axis commands (eg:- axis square, axis normal, etc), but none of them worked. I also noticed that the DataAspectRatio is different in these two axes objects when I used get(gca,'DataAspectRatio') .
Any help on solving this issue is greatly appreciated.
Thanks in advance...
Benjamin Avants on 15 May 2014
One potential way to solve this in a general case where the data in the 3D plot will be different for each use would be to generate a new figure with the 3D plot and use its parameters to set the parameters of the embedded axis. This is assuming that the new plot looks the way you would like.
You can generate the new figure with its visible property set to 'off' so that it does not appear on the screen.
Maybe something like this...
f = figure('Visible','off');
plot3(...) % Ellipses '...' are used as place holders for code in this example
a = get(f,'Children');
myXLim = get(a,'XLim');
myYLim = get(a,'YLim');
I think you've touched on most of the properties that are likely to affect the display but here are the ones that come to mind for me:
X, Y, & Z Lim; DataAspectRatio; PlotBoxAspectRatio; CameraPosition; & CamerViewAngle
I would suggest looking up axes properties in the MATLAB documentation.
Within the tolerance of your GUI, you can also change the size of the embedded plot as follows:
position = get(handles.myAxis,'Position');
position2 = get(a,'Position');
position(3:4) = position2(3:4);
This method assumes that the new plot and your axis are close to the same size and that they both use the same units for positioning (not normalized, but Characters or Pixels for instance). This might prevent some of the cropping you've seen in your GUI axis.
If you are likely to be plotting the same (or very similar) data in your GUI, you could just play around with the settings until the display looks correct and then hard code them into your program. While your GUI is running, you can query your axis' current properties with its handle.
myAxis = findall(0,'Tag','TheTagISetForMyAxis'); % Get the handle
get(myAxis) % Query and display the properties
You can also set the properties manually in the same way using the handle and set() function.
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!