MATLAB: Struggling with slider control

slider gui

Hi. I have been struggling all morning with this and have thrown in the towel. I am plotting histograms of typically 16bit images, and through the use of a slider control, I want to be able to scan the histogram (and draw a red line) and obtain the intensity (x-axis) as well as the counts at that intesnity (y-axis)
The problem comes when I use binning other than 1 bin = 1 intensity level.
for this example, my max pixel value is 22752, and I want to use 1000 bins.
maxval=max(IM(:))
The histogram is obtained with:
nbins=1000;
[counts,x] = imhist(IM,nbins);
%save data globally
setappdata(0,'Counts',counts);
setappdata(0,'X',x);
setappdata(0,'MaxVal',maxval);
grid on;
hold on;
xlim([0,maxval]);
ylim('auto');
bar(x,counts,'b','EdgeColor','b');
Then on the slider callback, I now calculate the binwidth (should be 22752/1000=22.75, and take the ceiling of it), note my nbins is in an editBin field.
binwidth=idivide(uint32(maxval), uint32(str2num(get(handles.editBin,'String'))), 'ceil')
I then setup the slider:
sliderMax=nbins
sliderMin=1;
set(handles.slider1,'Max',sliderMax);
set(handles.slider1,'Min',sliderMin);
sliderStep = [1, 10] / (sliderMax - sliderMin);
set(handles.slider1,'SliderStep',sliderStep);
So when its clicked, return back the value
p=get(hObject,'Value') %Get value from slider
Drawline and update Intensity and Freq edit boxes:
p=round(p) %in case non integer
x(p) %This is the value of the intensity
plot([x(p) x(p)],[0 max(counts)],'r', 'LineWidth',1);
guidata(hObject, handles);
drawnow;
hold off;
maxval
F=counts(p);
set(handles.editFreq,'String',num2str(F))
set(handles.editIntesnity,'String',num2str(p*binwidth,'%.0f'));
But it all seems messed up.
Are there any obvious mistakes please.
Thankyou
Jason

Best Answer

What aspect exactly is "messed up"? Is your red line updating correctly when you move the slider? Given how far along its maximum length the slider carat is compared to how far into the graph your red line is I suspect not.
If you put a breakpoint in your slider callback is the value 'p' what you would expect it to be? I always combine a slider with an editbox reporting the slider's value so that I know the slider is reporting the correct value (and so I can type in a specific value if I want). Also I assume 'x' in your callback is what you expect it to be? Your code doesn't show how the callback gets hold of 'x'.
Judging from your screenshot I would guess it is probable that your edit box fields are updating correctly but your red-line is incorrectly placed though I don't see anything immediately wrong with that code.
As an aside...for your intensity edit-box, is there a reason you are using 'p*binwidth' instead of just x(p) which your earlier comment says is the intensity level? I haven't put the thought in to see if they are the same thing, but x(p) would seem simpler if that is the value you want to put on the gui.
If these are not the same thing then that may be your problem as you are plotting the line at x(p). If you plot the line at 'p*binwidth' does it then match up correctly?