Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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});
    end
    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
        gui_mainfcn(gui_State, varargin{:});
    end
    % 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
    handles.range=50;
    % Update handles structure
    guidata(hObject, handles);
    % UIWAIT makes cosscale wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    fplot('cos', [0, 10]);
    xlabel('x');
    ylabel('cos(x)');
    % --- 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]);
    end
    %---the actual plotting functioin
    function myplot(hObject, handles)
    str=get(handles.edit1, 'String');
    fh=str2func(['@(x)',str]);
    x=0:0.1:handles.range;
    y = fh(x);
    plot(x, y, 'Parent', handles.axes1, 'Color', handles.cr);

????

0 Comments

Nora

Products

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?

6 Comments

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.

Nora 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.

Image Analyst

Contact us