How can I create a GUI to change the bins of a histogram?

조회 수: 6 (최근 30일)
William Chuirazzi
William Chuirazzi 2021년 6월 3일
댓글: Amit 2021년 6월 5일
I am trying to teach myself how to create MATLAB GUIs and, after studying some examples, have run into some trouble and need to request help.
The number of bins in a histogram can be specified in the input for the histogram. I want to create a GUI with a slider that the user can move to change the bin size, updating the histrogram. I would like the slider to be part of the figure containing the histogram plot.
My code is:
data=rand(100,1);
n=50;
fig=histogram(data,n);
xlabel('Number')
ylabel('Frequency')
S.fh = figure('units','pixels',...
'position',[400 400 220 40],...
'menubar','none',...
'name','Bins',...
'numbertitle','off',...
'resize','off');
S.sl = uicontrol('style','slide',...
'unit','pixel',...
'position',[10 10 200 20],...
'min',1,'value',50,'max',100,...
'callback',{@sl_call,n});
function [] = sl_call(varargin) % Callback for the slider.
[h n] = deal(varargin{[1;3]}); % Get the calling handle and structure.
set(n,get(h,'val'))
end
However, I get the following error:
function [] = sl_call(varargin)
Error: Function definition not supported in this context. Create functions in code file.
Can someone advise me on how to fix this error and how to get the GUI working?
Thanks in advance for any help!
  댓글 수: 1
Amit
Amit 2021년 6월 5일
Creating GUI is very easy when you use GUIDE command.
Then simple job remains is of putting small pieces of codes under callback of various graphical objects such as buttons etc.
You can send me code that you are having problem with and test images those you are using on amit.kenjale@gmail.com, I will update your code, comment each line for your understanding and try this updated code on images those you will send.

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

답변 (1개)

Steven Lord
Steven Lord 2021년 6월 3일
Change the NumBins property of the histogram object in your slider callback if you want to explicitly control the number of bins.
If you just want "more" or "fewer" bins but don't necessarily need to control the exact number of bins call morebins or fewerbins on the histogram's handle or click the arrow in the figure toolbar then right-click on the histogram and select "More Bins" or "Fewer Bins".
  댓글 수: 1
William Chuirazzi
William Chuirazzi 2021년 6월 3일
Thanks, Steven.
I do want to be able to control the exact number of bins with the slider so "More Bins" or "Fewer Bins" is out for me. I have taken your suggestion about NumBins to modify my code as shown below, but still get the same error. If you could please help me see where I am going wrong, I'd appreciate it. Thanks!
data=rand(100,1);
n=50;
fig=histogram(data,n);
xlabel('Number (nm)')
ylabel('Frequency')
S.fh = figure('units','pixels',...
'position',[400 400 220 40],...
'menubar','none',...
'name','Bins',...
'numbertitle','off',...
'resize','off');
S.sl = uicontrol('style','slide',...
'unit','pixel',...
'position',[10 10 200 20],...
'min',1,'value',50,'max',100,...
'callback',{@sl_call,n});
function [] = sl_call(varargin)
% Callback for the slider.
[h fig.NumBins] = deal(varargin{[1;3]}); % Get the calling handle and structure.
set(fig.NumBins,get(h,'val'))
end

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by