2016-08-19 23 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) 

単位ステップのために、私は

答えて

1

あなたがValueで指定したいと思うでしょう、私は間違っているつもりどこにどれリードが参考になりますmaxvalue-minvalue

を選択する必要があり、ので、私は1/9をchosedていますCreateFcnと同様に、デフォルトでは0が/Maxの範囲外になり、uicontrolがレンダリングされないためです。また、私はあなたが常に整数(ドラッグしても)するスライダーの値を強制したい場合、あなたは内Valueプロパティを丸めることができます。また

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]); 

同様 CreateFcn内から SliderStepを設定することをお勧めしますスライダのコールバック

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 
関連する問題