Simple question with slider in GUI interface

4 views (last 30 days)
I have a GUI interface with an edit text box (tag edit1) and a slider (tag slider 1). At a certain point of the program, a number is shown in the edit text box. What I want to do is to increase by 1 unit the number in edit1 when pressing the up arrow of the slider and decrease it by 1 unit when pressing the down arrow of the slider. What is the code to write into the function
function slider1_Callback(hObject, eventdata, handles)
? Thank you

Accepted Answer

Image Analyst
Image Analyst on 2 Oct 2014
Try this:
% Get string from edit box.
editBoxValue = get(handles.edit1, 'String');
% Convert string to number.
theNumber = str2double(editBoxValue);
% Increment the number.
theNumber = theNumber + 1;
% Send new number back into the edit box.
set(handles.edit1, 'String', theNumber);
You might want to consider use of a slider instead of edit boxes and keystrokes.
  4 Comments
Image Analyst
Image Analyst on 2 Oct 2014
Edited: Image Analyst on 2 Oct 2014
Here's a snippet from one of my programs. I have 3 sliders. In each slider's callback, it calls this function which reads all the sliders and updates the label above the slider with the current value of the sliders.
% Gets called in the callback of each of the 3 sliders on the GUI.
% Reads the value of each slider and sets the
% caption (static text label) above each slider.
function SliderMoved(handles)
try
% Set the caption for the smallest dot.
scrollbarValue = round(get(handles.sldSmallest,'Value')); % Round to nearest integer.
set(handles.sldSmallest, 'Value', scrollbarValue); % Send rounded value back in.
caption = sprintf('Smallest Allowable Dot = %d pixels.', scrollbarValue);
set(handles.txtSmallest, 'string', caption);
% Set the caption for the largest dot.
scrollbarValue = round(get(handles.sldLargest,'Value')); % Round to nearest integer.
set(handles.sldLargest, 'Value', scrollbarValue); % Send rounded value back in.
caption = sprintf('Largest Allowable Dot = %d pixels.', scrollbarValue);
set(handles.txtLargest, 'string', caption);
% Set the caption for the threshold.
scrollbarValue = round(get(handles.sldThreshold,'Value')); % Round to nearest integer.
set(handles.sldThreshold, 'Value', scrollbarValue); % Send rounded value back in.
caption = sprintf('Threshold = %d gray levels.', scrollbarValue);
set(handles.txtThreshold, 'string', caption);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from SliderMoved()
Note: You need to set up the slider step first to get exactly 1.
sliderMin = get(handles.slider1, 'Min');
sliderMax = get(handles.slider1, 'Max');
stepValue = 1 / (sliderMax - sliderMin);
set(handles.slider1, 'SliderStep', [stepValue, 3*stepValue]);
You might do that in the yourGui_OpeningFcn() function.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by