How do I dynamically add new subplots to a figure in MATLAB 7.9 (R2009b)?
22 views (last 30 days)
Show older comments
MathWorks Support Team
on 19 Dec 2016
Answered: MathWorks Support Team
on 19 Dec 2016
Why is there only one subplot axis seen instead of two when I execute the following code?
>> figure(1), subplot(1,1,1)
>> subplot(2,1,2)
I am expecting my current axis to remain as I increase the number of subplots that I add to my figure. My use case is that I would like to create figures dynamically in a GUI using GUIDE, such that when the user clicks a button, it will add new subplots to the current figure.
Accepted Answer
MathWorks Support Team
on 19 Dec 2016
When you are creating new subplots, such as subplot(1,1,1), then subplot(2,1,2), the new subplot specification causes a new axis to overlap an existing axis, so the existing axis is deleted - unless the position of the new and existing axis are identical.
However, if you would like to dynamically create subplots, you can first get a handle to the current axes, then change the position of the axes when new you would like to create new axes. Start with a plot with one axis:
figure1 = figure; %get a handle to a new figure
plot(1:10)
axes1 = gca; %get a handle to this axis
Now, to create a new subplot below this one, change the axes position for the current axes:
% move the axes1 position
set(axes1,'OuterPosition',[0 0.5 1 0.5]);
% Create 2nd axes and set the position below the first axis
axes2 = axes('Parent',figure1,'OuterPosition',[0 0 1 0.5]);
plot(axes2,1:20,1:20)
Where 'OuterPosition' is specified as[left bottom width height]
It may also be useful to use the command PLOTTOOLS, which will allow you to create plots, and then generate m code to view those plots (in plottools gui, File > Generate code). This will allow you to create and view examples with the axes 'position' property.
0 Comments
More Answers (0)
See Also
Categories
Find more on Subplots 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!