2016-08-19 117 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 
相關問題