MATLAB: Slider GUI error with non-extreme values

slider gui error

Hello all!
I am trying to create a GUI where the slider updates the image slice displayed. I've looked at various examples but am not totally successful yet. With the code below, the slider displays the correct image at the first (1) and last (33) positions. However, any other location gives the error message "Subscript indices must either be real positive integers or logicals."
% --- Executes just before MagicWandGUI is made visible.
function MagicWandGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to MagicWandGUI (see VARARGIN)
% Add gen MATLAB path
addpath(genpath('C:\Users\Student\Desktop\DECT_research'));
% Load Iformatted40 {n} where n1:33; target slice is 17
load('C:\Users\Student\Desktop\DECT_research\Code\Iformatted40.mat')
% Set number of slides
IM_MAX = 33;
% Set default IMG to zeros
axes(handles.axes1)
imshow=zeros(512);
% Choose default command line output for MagicWandGUI
handles.output = hObject;
handles.Iformatted40 = Iformatted40;
handles.IM_MAX = IM_MAX;
% Set limits on slider
set(handles.slider1, 'Min', 1);
set(handles.slider1, 'Max', handles.IM_MAX);
set(handles.slider1, 'Value', 17);
set(handles.slider1, 'SliderStep', [1/handles.IM_MAX , 10/handles.IM_MAX]);
% Update handles structure
guidata(hObject, handles);
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
SliderLocation = get(handles.slider1,'Value');
axes(handles.axes1)
Iformatted40 = handles.Iformatted40;
imshow(Iformatted40{SliderLocation}, [-335 223])
Set(handles.slider1,'Value',SliderLocation)
The curious thing is that the slider does take 33 steps to go from one end to the other. Where have I made a mistake? Also, I intended for the axes to be all dark (zeros) upon loading, but that also isn't happening.
Thank you so much!

Best Answer

Brian - if you examine the SliderLocation whenever you press the up or down arrow of the slider, you will notice that it is a non-integer except for when it the slider is in the first or last position (in which case it is either 1 or 33). In fact, the kth step has a SliderValue of
k*(IM_MAX-1)/(IM_MAX) + 1
for k greater than one a you have set the minorStep within the StepSize to be
1/handles.IM_MAX
which is the _ fraction of the slider range by which the Value property increases or decreases when the user clicks one of the arrow buttons_. To get something more resembling an integer, you could change this minor step size to
1/(handles.IM_MAX-1)
and now when you use the arrows, the slider value will more likely be an integer. This won't be guaranteed since the user could move the slider him- or herself which won't guarantee the inter value. So what we could do is guard against this. In your slider1_Callback do the following
function slider1_Callback(hObject, eventdata, handles)
SliderLocation = round(get(handles.slider1,'Value'));
set(handles.slider1,'Value', SliderLocation);
% et.
Try making both of the above changes and see what happens! As for ensuring that your first image is black, try instead
axes(handles.axes1)
image(zeros(512,512,3));
in the MagicWandGUI_OpeningFcn function. (Just creating a 3D image instead of the 2D one.)