Novice question - Push button to display y intercept
Show older comments
Hi all! Novice user. I would like a push button to display a y-intercept on a simple plot. The following code will display the y intercept, but it dissapears if you move a slider. I have tried a few 'if' statements but with no success. Also, for some reason, GUIDE does not like 'end' in some statements. Here is the code and the files are attached. Very grateful as I am just starting to make progress here :)
function varargout = gui_experiment_2sliders1plot(varargin)
% GUI_EXPERIMENT_2SLIDERS1PLOT MATLAB code for gui_experiment_2sliders1plot.fig
% GUI_EXPERIMENT_2SLIDERS1PLOT, by itself, creates a new GUI_EXPERIMENT_2SLIDERS1PLOT or raises the existing
% singleton*.
%
% H = GUI_EXPERIMENT_2SLIDERS1PLOT returns the handle to a new GUI_EXPERIMENT_2SLIDERS1PLOT or the handle to
% the existing singleton*.
%
% GUI_EXPERIMENT_2SLIDERS1PLOT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_EXPERIMENT_2SLIDERS1PLOT.M with the given input arguments.
%
% GUI_EXPERIMENT_2SLIDERS1PLOT('Property','Value',...) creates a new GUI_EXPERIMENT_2SLIDERS1PLOT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_experiment_2sliders1plot_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_experiment_2sliders1plot_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gui_experiment_2sliders1plot
% Last Modified by GUIDE v2.5 25-Mar-2024 08:51:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_experiment_2sliders1plot_OpeningFcn, ...
'gui_OutputFcn', @gui_experiment_2sliders1plot_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
function update_plot(handles)
time= 0:1/500:2;
A = get(handles.horiz_slider, 'Value');
F = get(handles.vertical_slider, 'Value');
MySignal = A*sin(2*pi*F*time);
plot(handles.axes1, time, MySignal);
function intercept_plot(handles)
time= 0:1/500:2;
yline(0.2, 'r--', 'Test Line');
plot(handles.axes1, time, yline);
% --- Executes just before gui_experiment_2sliders1plot is made visible.
function gui_experiment_2sliders1plot_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gui_experiment_2sliders1plot (see VARARGIN)
% Choose default command line output for gui_experiment_2sliders1plot
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_experiment_2sliders1plot wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_experiment_2sliders1plot_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function horiz_slider_Callback(hObject, eventdata, handles)
% hObject handle to horiz_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
update_plot(handles);
% --- Executes during object creation, after setting all properties.
function horiz_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to horiz_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function vertical_slider_Callback(hObject, eventdata, handles)
% hObject handle to vertical_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
update_plot(handles);
% --- Executes during object creation, after setting all properties.
function vertical_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to vertical_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in y_intercept1.
function y_intercept1_Callback(hObject, eventdata, handles)
% hObject handle to y_intercept1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of y_intercept1
intercept_plot(handles)
1 Comment
A usage note: yline plots a line on its own, so you shouldn't do plot(...,yline).
In other words, you can just do this:
yline(0.2, 'r--', 'Test Line');
instead of doing this:
time= 0:1/500:2;
yline(0.2, 'r--', 'Test Line');
plot(handles.axes1, time, yline);
Even better would be to pass the axes handle as the first argument to yline:
yline(handles.axes1, 0.2, 'r--', 'Test Line');
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!