Simple question with slider in GUI interface

조회 수: 3 (최근 30일)
aurc89
aurc89 2014년 10월 2일
편집: Image Analyst 2014년 10월 2일
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

채택된 답변

Image Analyst
Image Analyst 2014년 10월 2일
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
Joseph Cheng
Joseph Cheng 2014년 10월 2일
I wonder if you need a slider if you're just using the arrows. two push buttons would do exactly what you want. Or a quick modification of what Image Analysis has supplied
%if you edit the slider to nominal value at 1 and has range/step of 0:1:2
editBoxValue = get(handles.edit1, 'String');
% Convert string to number.
theNumber = str2double(editBoxValue);
%Determine up or down and increment
theNumber = theNumber + get(handles.slider1,'value')-1;
% Send new number back into the edit box.
set(handles.edit1, 'String', theNumber);
%reset slider to 1;
set(handles.slider1,'value',1);
Image Analyst
Image Analyst 2014년 10월 2일
편집: Image Analyst 2014년 10월 2일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genomics and Next Generation Sequencing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by