Setting the variable range of Slider in GUI

4 Ansichten (letzte 30 Tage)
Aleksandra Pestka
Aleksandra Pestka am 15 Jan. 2018
Hello everyone! I'd like to set a variable MIN and MAX of slider in GUI. These values will be entered by user in 'cell_min' and 'cell_max'. The result of position of slider appears in 'cell_output'. I also enclose a adequate part of my code below:
function slider1_Callback(hObject, eventdata, handles)
variableMax=str2num(get(handles.cell_max,'String'));
variableMin=str2num(get(handles.cell_min,'String'));
set(handles.slider1,'MIN',variableMin,'MAX',variableMax);
valor=get(hObject,'value');
set(handles.cell_output,'string', num2str(valor));
guidata(hObject,handles);
For reasons unknown for me, it does not work. The alert which appears:
Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range
Control will not be rendered until all of its parameter values are valid
The slider also disappears when I try to set some value by it.
What am I supposted to do?
I'll do appreciate any help.
  5 Kommentare
Jan
Jan am 17 Jan. 2018
@Aleksandra: Set the Min and Max during the creation of the slider. Are you working with GUIDE? Then the OpeningFcn might be the best location - if the values for Min and Max are known already. If not, set them exactly in this part of the code, where they are created.
Adam
Adam am 17 Jan. 2018
If the user can change the min and max then do it in the callback of whatever UI component they use to do this, making sure you set the value at the same time.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 15 Jan. 2018
Read the warning message:
Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range
Control will not be rendered until all of its parameter values are valid
When the value is outside of the min-max range then this warning is shown and the slider is not rendered (shown on screen). Clearly your value is outside of this range. If you are dynamically changing the min/max values then you need to consider the order in which you change them, and might need to change the value as well to ensure that it is always between min and max.
  11 Kommentare
Stephen23
Stephen23 am 17 Jan. 2018
Bearbeitet: Stephen23 am 17 Jan. 2018
"I've thought that a position of sliders should be compatible with painted area like in the example above"
Yes, correct: you need to change some variables' values using the slider position values. These variable will be internal to your GUI somehere (exactly how depends on your GUI design).
"Thus, the limits of sliders should be the same as the domain on the graph."
No. The slider end limits do not need to change.
Aleksandra Pestka
Aleksandra Pestka am 17 Jan. 2018
I've realised that I only need to modify the slider's value properly to get a suitable range. I've changed the code like this:
function slider1_Callback(hObject, eventdata, handles)
variableMax=str2num(get(handles.cell_max,'String'));
variableMin=str2num(get(handles.cell_min,'String'));
value=get(hObject,'value');
value_slider1=value*(variableMax-variableMin)+variableMin;
set(handles.cell_output,'string', num2str(value_slider1));
guidata(hObject,handles);
And now it's working awsome :)
@Stephen Cobeldick Thanks a lot for your help! Your tips were really useful.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by