Slider in GUI

1 view (last 30 days)
Sara Marques
Sara Marques on 19 Aug 2011
Hello
I'm having trouble getting a slider to work in a GUI.
I currently have 1 slider in my GUI, that is suposed to change its values everytime a different variable is chosen in a popupmenu.
After a certain variable is chosen, the slider Min and Max properties would be changed to the variable Min and Max and the slider should run through all the variable values.
I've tried to change the slider Min, Max and Step properties by writing
set(handles.slider1,'Max',variableMax);
set(handles.slider1,'Min',variableMin);
but its not working.
Is it possible to make the slider work this way? And if it is, how can i do it?
Thanks in advance.
Sara

Accepted Answer

Paulo Silva
Paulo Silva on 19 Aug 2011
not working? how can we help if you don't provide enough details (error message at least), please be sure that variablemax and min are numbers (use str2num if it's a string) not strings or a cell. Also remeber to update the handles structure after setting the new values.
prompt = {'Enter min:','Enter max:'};
dlg_title = 'min=?,max=?';
num_lines = 1;
def = {'0','1'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
%answer is a cell with two strings
%You must choose the cell element answer{n} , that gives you the string
%first one is the min and the second is the max
%Now you need to convert string to number, that can be done using str2num
variableMin=str2num(answer{1});
variableMax=str2num(answer{2});
set(handles.slider1,'Max',variableMax);
set(handles.slider1,'Min',variableMin);
% Update handles structure
guidata(hObject, handles);

More Answers (1)

Sara Marques
Sara Marques on 19 Aug 2011
Hello Paulo
Thanks for the quick reply.
Heres the code I wrote to try to get the slider working as intended:
function popupmenu5_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu5 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu5
sliderNum=get(handles.popupmenu5,'Value');
var=handles.input(sliderNum,:);
variableMin=min(var);
variableMax=max(var);
set(handles.slider1,'Max',variableMax);
set(handles.slider1,'Min',variableMin);
guidata(hObject,handles);
When i choose a variable listed in the popupmenu, the code gets the correspondent variable row in the input matrix. I get the warning:
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
Im new in developing GUIs, so I dunno what im doing wrong.
Thanks in advance
Sara
  2 Comments
Paulo Silva
Paulo Silva on 19 Aug 2011
When you set a value for the slider that is outside of its limits max and min that slider give the error and is invisible, always set a value inside the limits.
Sara Marques
Sara Marques on 19 Aug 2011
hello again
You're right Paulo. I wasn't updating the slider value. Works fine now.
Thanks a lot for the help
Sara

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!