简体   繁体   English

设置滑块步骤以及默​​认的最小值和最大值

[英]Settling slider steps and default min and max values

I am trying to set min and max values of my slider when it creates. 我试图在创建滑块时设置其最小值和最大值。

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

but when the GUI opens, it throws and error and slider disappears 但是,当打开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 

And i am trying to set the slider steps as 1. even when it is dragged or when the increase/decrease button is used. 而且我正在尝试将滑块步数设置为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)

I have chosed 1/9 because, for unit steps i need to choose maxvalue-minvalue 我选择1/9是因为,对于单位步骤,我需要选择maxvalue-minvalue

Any leads on where i am going wrong will be helpful 任何有关我要去哪里的线索都会有所帮助

You will want to specify the Value in your CreateFcn as well because by default, the value will be 0 which is outside of your Min / Max range which will cause the uicontrol to not be rendered. 您还需要在CreateFcn指定“ Value ,因为默认情况下,该值将为0 ,该值超出您的“ Min / Max范围,这将导致不呈现uicontrol Also, I would recommend setting the SliderStep from within the CreateFcn as well 另外,我建议设置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]);

Also if you want to force the slider value to always be an integer (even when dragged), you can round the Value property inside of the slider's callback 另外,如果您想强制滑块值始终为整数(即​​使在拖动时也是如此),则可以在滑块的回调内部四舍五入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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM