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

조회 수: 1 (최근 30일)
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);
????

채택된 답변

Image Analyst
Image Analyst 2013년 12월 1일
What happens when you set breakpoints and step through it with the debugger?
  댓글 수: 6
Nora
Nora 2013년 12월 3일
I fixed the problem. Thank you.
Image Analyst
Image Analyst 2013년 12월 3일
So did I, in the answer to your duplicate question. I attach here again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by