MATLAB Answers


Using a slider to move a cosine curve from the range of 1 to 100? - Homework

Asked by Nora
on 1 Dec 2013
Latest activity Commented on by Image Analyst
on 3 Dec 2013

I am writing a GUI program where a slider has a range from 1 to 100, with default value of 50 at the beginning. The plot will show a cosine curve with x-values from 0 to the value set by the slider.

This is what I have so far. I am not sure what is wrong because I can't get the slider to work???

function varargout = cosscale1(varargin)
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @cosscale_OpeningFcn, ...
                       'gui_OutputFcn',  @cosscale_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);
    if nargin && ischar(varargin{1})
        gui_State.gui_Callback = str2func(varargin{1});
    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
        gui_mainfcn(gui_State, varargin{:});
    % End initialization code - DO NOT EDIT
    % --- Executes just before cosscale is made visible.
    function cosscale_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
    set(handles.slider1, 'min', 1, 'max', 100, 'value', 50);
    %set the corresponding plotting range
    % Update handles structure
    guidata(hObject, handles);
    % UIWAIT makes cosscale wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    fplot('cos', [0, 10]);
    % --- Outputs from this function are returned to the command line.
    function varargout = cosscale_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;
    function edit1_Callback(hObject, eventdata, handles)
    myplot(hObject, handles);
    % --- Executes on slider movement.
    function slider1_Callback(hObject, eventdata, handles)
     of slider
    handles.range=get(handles.slider1, 'Value');
    %plot the curve
    myplot(hObject, handles);
    guidata(hObject, handles);
    % --- Executes during object creation, after setting all properties.
    function slider1_CreateFcn(hObject, eventdata, handles)
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    %---the actual plotting functioin
    function myplot(hObject, handles)
    str=get(handles.edit1, 'String');
    y = fh(x);
    plot(x, y, 'Parent', handles.axes1, 'Color',;




No products are associated with this question.

1 Answer

Answer by Image Analyst
on 1 Dec 2013
 Accepted answer

What happens when you set breakpoints and step through it with the debugger?


Image Analyst
on 3 Dec 2013

This would be so much easier if you would attach your files, so we can fix them and reattach them.

on 3 Dec 2013

I fixed the problem. Thank you.

Image Analyst
on 3 Dec 2013

So did I, in the answer to your duplicate question. I attach here again.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today