Extracting the value of a slider

조회 수: 29 (최근 30일)
Gar
Gar 2011년 6월 14일
댓글: Chanchal Dass 2017년 9월 28일
I created a slider using the GUIDE. I want to use the slider's numeric output as the input to another m-file, but I do not know how to get it so that I can use the sliders output for another file.
I have been looking around a lot and it seems that I have to do something with a callback function but I am not sure if I should put this in my slider code or my other m-file's code.

답변 (5개)

Doug Hull
Doug Hull 2011년 6월 14일
The value of a slider is available when you use the get command on the handle of the slider. For instance:
>> h = uicontrol('style','slider')
h =
0.0123
>> get(h,'value')
ans =
0
%Mess with slider a bit
>> get(h,'value')
ans =
0.1000
The trick for you is getting the handle of the slider. For that, we need to know how you made the slider. It is easiest to do this when you already know the handle to the slider. Otherwise findobj might be helpful, or look into the handles structure if using the GUIDE.

Gar
Gar 2011년 6월 17일
Here is the code that the GUIDE generated when I made the slider. I noticed that several times in the code the program gets that value of the slider, but I am not sure what I have to call in a seperate .m file to get this value. It seems like I should use the callback function but I am not sure what to enter for the input parameters. I tried to put 'return' after one of the spots where the program finds the value. I also tried to write a small function in this code that just found the slider value but I could not get it to work. Like I said, I am very new to matlab so any help would be greatly appriciated. Thanks!
% --- Outputs from this function are returned to the command line.
function varargout = WaveLength_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- 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)
%obtains the slider value from the slider component
sliderValue = get(handles.slider1,'Value');
%puts the slider value into the edit text component
set(handles.sliderValue_editText,'String', num2str(sliderValue));
% Update handles structure
guidata(hObject, handles);
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function sliderValue_editText_Callback(hObject, eventdata, handles)
% hObject handle to sliderValue_editText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%get the string for the editText component
sliderValue = get(handles.sliderValue_editText,'String');
%convert from string to number if possible, otherwise returns empty
sliderValue = str2num(sliderValue);
%if user inputs something is not a number, or if the input is less than 0
%or greater than 100, then the slider value defaults to 0
if (isempty(sliderValue) || sliderValue < 920 || sliderValue > 1000)
set(handles.slider1,'Value',940);
set(handles.sliderValue_editText,'String','940');
else
set(handles.slider1,'Value',sliderValue);
end
% Hints: get(hObject,'String') returns contents of sliderValue_editText as text
% str2double(get(hObject,'String')) returns contents of sliderValue_editText as a double
% --- Executes during object creation, after setting all properties.
function sliderValue_editText_CreateFcn(hObject, eventdata, handles)
% hObject handle to sliderValue_editText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
  댓글 수: 1
Chanchal Dass
Chanchal Dass 2017년 9월 28일
Majority people do not have this answer..

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


Matt Tearle
Matt Tearle 2011년 6월 14일
Your slider has a callback property that defines the function that will be called when the slider is moved. It's inside that function that you need to query the value property of the slider handle, as Doug says.
Example:
Callback function:
function slidercb(h,event)
val = get(h,'value')
% do something with the slider value
Somewhere else:
figure
uicontrol('units','normalized','position',[0.2,0.2,0.5,0.1],...
'style','slider','callback',@slidercb)

Gar
Gar 2011년 6월 20일
I figured it out. I kept my code for my WaveLength.m file the same. In the .m file that I wanted to obtain the value in I simply had to put
handles = guihandles(WaveLength)
%gets handles for figure and sets them to 'handles'
waveLen=get(handles.slider1,'Value')
% gets the number occupying the 'Value' of the figure 'slider1'
% in the
% handles structure

Oscar Machuca
Oscar Machuca 2013년 4월 2일
Im having the exact problem as you . But i didn't understand where did you write this code?
handles = guihandles(WaveLength)
%gets handles for figure and sets them to 'handles'
waveLen=get(handles.slider1,'Value')
% gets the number occupying the 'Value' of the figure 'slider1'
% in the
% handles structure
Do you write it on the same .m file or is it a new file . please post your code

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by