Missing subplots inside a panel
Show older comments
I am trying to draw some interfaces programmatically. I am blocked with a problem that I cannot fix. I need to draw several graphs with the characteristic that the number of subplots is variable and they have to be inserted inside a panel that uses a slider to enable the visualization of all of them. The problem is that although the drawing zone size is calculated correctly, it does not draw after position 0.63 (being 1 the end of the panel). Here is the code of my .m file and the images of the obtained result:
<<

>>
function SliderHor()
% make the GUI fullscreen
set(0,'units','pixels');
pix = get(0,'screensize');
w = pix(3);
h = pix(4);
set(gcf,'units','pixels','outerposition',[50 50 w-100 h-100]);
set(gcf, 'NumberTitle', 'off', 'Name','Graphs', 'MenuBar', 'none', 'ToolBar', 'none');
%Create the panel and the slider
panel1 = uipanel('Position',[0.05 0.15 0.9 0.8]);
panel2 = uipanel('Parent',panel1, 'Position',[0 0 2 1]);
s = uicontrol('Style','Slider','Parent',panel1,'Units','normalized',...
'Position',[0 0 1 0.05],'Value',0,'Callback',{@slider_callback1,panel2});
% paint the graphs
nReplicas = 10;
replicaW = 1/nReplicas;
for j=1:nReplicas
% data
x = 0: .1 : 2*pi;
y1 = cos(x);
y2 = sin(x);
% Draw each cell data
xpos = (j-1)*replicaW + replicaW/6;
subplot(1,2*nReplicas,(2*j)-1,'Parent',panel2,'Position',[xpos,0.2,replicaW/3,0.6]);
% Plot y1 vs. x (blue, solid) and y2 vs. x (red, dashed)
plot(x, y1, 'bs');
% Add title and axis labels
xlabel('angle')
ylabel('cos(x)')
title(sprintf('Replica %d',j))
xpos2 = (j-1)*replicaW + replicaW/6 + replicaW/3;
subplot(1,2*nReplicas,2*j,'Parent',panel2,'Position',[xpos2,0.2,replicaW/3,0.6]);
plot(x,y2,'r-','LineWidth',2);
xlabel('angle')
ylabel('sin(x)')
title(sprintf('Replica %d',j))
end
% callback function
function slider_callback1(src,eventdata,arg1)
val = get(src,'Value');
set(arg1,'Position',[-val 0 2 1])
end
end
Accepted Answer
More Answers (1)
Adam
on 23 Jun 2016
I very rarely use subplot and that behaviour I cannot understand on stepping through. However, you do not need to complicate issues by using subplot, just create axes and it seems to work fine - i.e.
Replace this:
subplot(1,2*nReplicas,(2*j)-1,'Parent',panel2,'Position',[xpos,0.2,replicaW/3,0.6]);
with this:
axes( 'Parent',panel2,'Position',[xpos,0.2,replicaW/3,0.6] );
and the equivalent for the 2nd plot of a pair too.
I would recommend keeping the axes handle you create and referring to it explicitly for plotting though anyway - e.g.
hAxes = axes( 'Parent',panel2,'Position',[xpos,0.2,replicaW/3,0.6] );
plot(hAxes, x, y1, 'bs');
% Add title and axis labels
xlabel(hAxes, 'angle')
ylabel(hAxes, 'cos(x)')
title(hAxes, sprintf('Replica %d',j))
5 Comments
Elisa Prada
on 23 Jun 2016
Adam
on 23 Jun 2016
I could see all the graphs when I ran it, but only when using axes. Using subplot the 7th and onwards started replacing removing the previous plots for reasons I couldn't understand.
Elisa Prada
on 23 Jun 2016
Elisa Prada
on 23 Jun 2016
Adam
on 23 Jun 2016
I am using R2016a, but I also tested in R2015a
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!