Simple question with slider in GUI interface

2 ビュー (過去 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 Center および File ExchangeGenomics 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