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

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);
????

 Accepted Answer

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

6 Comments

I get an error.
Error using load
Unable to read file 'cosscale1.fig': no
such file or directory.
Error in
graphics.internal.figfile.FigFile/read
(line 25)
hgDataVars = load(filename, '-mat',
'-regexp', '^hg[SO]');
Error in graphics.internal.figfile.FigFile
(line 98)
obj.read(file);
Error in C:\Program Files
(x86)\MATLAB\R2013a
Student\toolbox\matlab\graphics\hgload.p>hgload
(line 59)
Error in openfig (line 72)
[fig, savedvisible] = hgload(filename,
struct('Visible','off'));
Error in gui_mainfcn>local_openfig (line
286)
gui_hFigure = openfig(name, singleton,
visible);
Error in gui_mainfcn (line 159)
gui_hFigure =
local_openfig(gui_State.gui_Name,
gui_SingletonOpt, gui_Visible);
Error in cosscale1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Sort of looks like you have an m-file created by GUIDE but you are missing the associated cosscale1.fig file.
No that is in the same folder and it is present. I think in the original script is where I didn't correctly do the scaler for the GUI. Do you know how to do that for the problem? (Look at original question)
This would be so much easier if you would attach your files, so we can fix them and reattach them.
So did I, in the answer to your duplicate question. I attach here again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!