gca when using subplots

조회 수: 61 (최근 30일)
Jason
Jason 2017년 2월 16일
댓글: Sean de Wolski 2017년 2월 16일
Hi.
I have created a barchart on a figure and wanting to change the number of bins by adding a popupmenu to the figure. I assign the callback to this a function called setBins.
% Create pop-up menu on figure
popup = uicontrol('Style', 'popup',...
'String', {'5','10','20','50','100','200','500','1000','2000'},...
'Position', [10 0 80 30],...
'Callback', @setBins);
function setBins(source,event)
fig = gcf;
ax = fig.CurrentAxes;
cla(ax)
val = source.Value
indx = source.String
nbins=(indx(val,1))
nbins=str2double(nbins)
data=getappdata(0,'data'); %Data has already been saved using setappdata
[counts,xb]=hist(data(:,3),nbins); %IMHIST ONLY HANDLES 8 & 16 BIT IMAGES, NOT 12BIT
bar(log10(xb),counts,'b','EdgeColor','b'); %Replot with user defined number of bins
grid on;
hold on
xlim([min(log10(xb)) max(log10(xb))])
xlabel('log(Intensity)')
ylabel('Frequency')
set(gca,'fontsize',8)
hold off
My question is, this works fine with one plot on the figure. If however, this figure has two subplots and this is subplot(1,2,1), how would I get the above code just to apply to this subplot?
Thanks Jason

채택된 답변

Adam
Adam 2017년 2월 16일
편집: Adam 2017년 2월 16일
The subplot call includes a return argument which is the axes handle for that subplot. I would advise to always use this form and keep the axes handles in an array so that you can then do what you wish to them in future either all together or individually from the array.
Relying on gca or equivalent methods as to what happens to currently be in focus is always liable to lead to some seemingly strange bugs (strange at least until you become familiar with the cause and its effect)
  댓글 수: 3
Adam
Adam 2017년 2월 16일
'Callback', @(src,evt) setBins( src, evt, ax1 )
Then your function signature will be:
function setBins(source,event,ax)
and get rid of:
fig = gcf;
ax = fig.CurrentAxes;
Jason
Jason 2017년 2월 16일
Thanks Adam

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 2월 16일
Consider using the histogram function instead of hist. Once you've created a histogram plot, click the arrow icon on the figure toolbar then right-click on the histogram. Assuming you're using a numeric histogram rather than a categorical histogram, you will be able to select the "More bins" and "Fewer bins" options from the context menu that appears.
x = randn(10000, 1);
h = histogram(x);
plotedit on
% Now right-click on the histogram
You can also directly change the bin related properties of the histogram using the handle h if you want finer control over the number and location of the bins.
% Change the histogram to have only a few bins with narrower center bins
h.BinEdges = [-4 -2 -1 -0.5 0 0.5 1 2 4]
% Set the number of bins and let histogram choose their locations
h.NumBins = 11

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by