2016-08-19 56 views
0

我试图设置我的滑块创建时的最小值和最大值。建立滑块步骤和默认的最小值和最大值

function slider2_CreateFcn(hObject, eventdata, handles) 
% hObject handle to slider2 (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 
set(hObject, 'Max', 10, 'Min', 1); 

但是GUI打开时,它抛出和错误和滑块消失

Warning: slider control can not have a Value outside of Min/Max range 
Control will not be rendered until all of its parameter values are valid 
> In openfig at 135 
    In gui_mainfcn>local_openfig at 286 
    In gui_mainfcn at 234 
    In gui at 44 
Warning: slider control can not have a Value outside of Min/Max range 
Control will not be rendered until all of its parameter values are valid 
Warning: slider control can not have a Value outside of Min/Max range 
Control will not be rendered until all of its parameter values are valid 

,我试图设置滑块步骤1.它被拖到即使或者当增加/减少按钮被使用。

function slider2_Callback(hObject, eventdata, handles) 
% hObject handle to slider2 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% Hints: get(hObject,'Value') returns position of slider 
%  get(hObject,'Min') and get(hObject,'Max') to determine range of slider 

    set(handles.slider2, 'SliderStep' , [1/9,1/9]); 
sliderValue = get(handles.slider2,'Value'); 
set(handles.edit2,'String',sliderValue) 

我已经chosed 1/9,因为对单位梯级我需要选择maxvalue-minvalue

在我要去的地方错误的任何线索将有助于

回答

1

您将要指定在Value您的CreateFcn也是因为默认情况下,该值将是0,它不在您的Min/Max范围之内,这将导致uicontrol无法呈现。另外,我建议从CreateFcn内设置SliderStep以及

function slider2_CreateFcn(hObject, eventdata, handles) 
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor',[.9 .9 .9]); 
    end 

    set(hObject, 'Max', 10, 'Min', 1, 'Value', 1, 'SliderStep', [1/9 1/9]); 

另外如果你想迫使滑块值始终是一个整数(拖即使),就可以圆内部的Value财产滑块的回调

function slider2_Callback(hObject, eventdata, handles) 
    value = round(get(handles.slider2, 'Value')); 
    set(handles.slider2, 'Value', value); 

    % Do whatever you need to do in this callback 
end 
相关问题