Setting the variable range of Slider in GUI

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 Comments

@Aleksandra Pestka: this is not twitter. I removed the # symbols from your tags.
That is a very confusing slider callback! For a start, hObject and handles.slider1 are the same thing so use the same all over.
Secondly, why are you updating the slider min max in the slider's own callback, to values from some cell array elsewhere? It seems to be a very odd place to be doing this.
@Adam Where should I updating the slider min and max?
@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.
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.

Sign in to comment.

Answers (1)

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 Comments

I still don't have a clue what I should change in my code
Stephen23
Stephen23 on 15 Jan 2018
Edited: Stephen23 on 15 Jan 2018
@Aleksandra Pestka: imagine that value is currently 10, and then you try to set min & max to 2 & 8. Would value be in between min and max? No, it would not be, and then you will get the warning and the object will not be rendered.
Your task is to ensure that value is always in between min and max: this might mean moving max first, then the value, then min, or whatever sequence that suits the changes that you are making. Alternatively you can temporarily disable that warning, before setting the min, max, and value so that value is in between min and max, and then turning the warning back on.
I've tried to add default value for slider:
set(handles.slider1,'Value',get(handles.slider1,'Min'));
But after then, the slider's position is always Min, even if I want to change it in GUI. What to do in that case?
@Aleksandra Pestka: if you put that code inside the callback then every time the callback runs it will set the value back to that "default".
Adam's comment is important: it is very strange to be altering the min/max values of a slider within a callback for that slider. It is not clear what you want to achieve with this.
I'm working on project connected with integral of function. User enters a function and the range of X. The function is shown on the graph included domain. In the next step user can choose (using sliders) a narrower range for which an integral will be calculated. The area between function and X-axis will be painted (of course in the previous choosen narrow range). Only I have to do now is to find out how I should set the edge values of sliders and in which callbacks write the code. Hope that I express myself pretty clearly.
Stephen23
Stephen23 on 16 Jan 2018
Edited: Stephen23 on 16 Jan 2018
@Aleksandra Pestka: you wrote "In the next step user can choose (using sliders) a narrower range for which an integral will be calculated. The area between function and X-axis will be painted (of course in the previous choosen narrow range). Only I have to do now is to find out how I should set the edge values of sliders and in which callbacks write the code"
I have two questions:
  1. How many sliders do you have?
  2. Are you trying to change a sliders min/max limits from within a callback of that same slider?
As far as I can understand it, to adjust the integral range you do not need to adjust any slider min/max limits. Two sliders could be used for setting the integral limits, but this does not require changing their end limits dynamically.
@Stephen Cobeldick - I have 2 sliders. And yes, I am trying to change a slider max/min limits in the same slider's callback( I do ask for your understanding, cause I'm a beginner in Matlab and a lot of, sometimes very trivial things, are unknown for me). I've also attached a sreenshoot of a task which I model myself on.
Stephen23
Stephen23 on 17 Jan 2018
Edited: Stephen23 on 17 Jan 2018
@Aleksandra Pestka: The task description:
does not require that you change the end limits of the sliders dynamically. It asks that you use the slider values to change the limits of the integral: these are going to be some variables inside your GUI, but they are NOT the limits of the sliders themselves.
I've thought that a position of sliders should be compatible with painted area like in the example above. Thus, the limits of sliders should be the same as the domain on the graph. So have I misinterpreted the task?
Stephen23
Stephen23 on 17 Jan 2018
Edited: Stephen23 on 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.
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.

Sign in to comment.

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Asked:

on 15 Jan 2018

Commented:

on 17 Jan 2018

Community Treasure Hunt

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

Start Hunting!