Novice question - Push button to display y intercept
1 view (last 30 days)
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
Voss
on 25 Mar 2024
Edited: Voss
on 25 Mar 2024
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
Voss
on 25 Mar 2024
The reason the red line disappears when you move a slider is that plot replaces whatever is in the axes with the newly plotted line. Typically, you could use hold to keep whatever is in the axes when plotting, but in this case that would keep not only the red line but also the sine wave line.
There are various ways to have the sine wave line update while keeping the red yline. The easiest way is, after calling plot, if the y-intercept radiobutton is checked, call intercept_plot to create the yline again.
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);
if get(handles.y_intercept1,'Value')
intercept_plot(handles)
end
function intercept_plot(handles)
yline(handles.axes1, 0.2, 'r--', 'Test Line');
(Note that this also has the effect of removing the red yline after the radiobutton is deselected, not immediately but the next time update_plot is called.)
The best solution (in my opinion) is to create the sine line and the horizontal line in the OpeningFcn using line not plot, and update them when necessary by setting their XData/YData property and setting the red line's Visible property in the radiobutton callback.
9 Comments
Voss
on 26 Mar 2024
Create handles.y_line with NaN or 0 (using NaN generates a warning in my MATLAB version) data in the Opening function:
% create the lines:
handles.sine_line = line(handles.axes1, NaN, NaN, 'Color',[0 0.447 0.741]);
handles.cos_line = line(handles.axes1, NaN, NaN, 'Color','b', 'LineStyle','--');
handles.y_line = yline(handles.axes1,0,'--r','Test Line');
Then, in your intercept slider callback, set its Value:
handles.y_line.Value = get(handles.intercept_slider, 'Value');
You can also initialize the handles.y_line to have the right value by calling the intercept slider callback from the Opening function, like is done already by calling update_plot and y_intercept1_Callback to initialize the other stuff. Or you can get the intercept slider Value and use that, like you are doing now.
The key to getting it to change when the slider is interacted with is to define the slider's callback and set handles.y_line.Value in that callback function.
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices 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!