How can I create a GUI to change the bins of a histogram?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
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
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".
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!