设置滑块步数和默认的最小值和最大值

时间:2016-08-19 08:54:22

标签: matlab slider matlab-guide

我试图在创建滑块时设置最小值和最大值。

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/9,因为对于单位步骤我需要选择maxvalue-minvalue

我出错的任何线索都会有所帮助

1 个答案:

答案 0 :(得分:1)

您还需要在Value中指定CreateFcn,因为默认情况下,值0超出Min / {{1范围会导致Max无法呈现。另外,我建议您在uicontrol内设置SliderStep

CreateFcn

此外,如果要强制滑块值始终为整数(即​​使在拖动时),也可以对滑块回调内的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
相关问题