Novice question - Push button to display y intercept

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

Sign in to comment.

 Accepted Answer

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

Hello again Voss and thanks very much for your help! I understand the above and it has worked.
Do you have an example of your preferred solution? Just so I can compare both methods of programming?
Sure, here you go (m-file attached; fig-file is the same as what's attached to the question).
Hi Voss, thanks again, it runs. My task today is to take this apart so I can understand what the code is doing
Let me know if you have any questions.
If my original answer solved your question, please "Accept" it. Thanks!
Hi Voss
So the next issue I have is trying to plot two waves using the above example:
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 26-Mar-2024 16:45:39
% 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
% --- 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;
% create the lines. NaN is used to create a Not-a-Number array,
% which returns NaN rather than garbage if an x or y value does not exist:
handles.sine_line = line(handles.axes1, NaN, NaN, 'Color',[0 0.447 0.741]);
handles.cos_line = line(handles.axes1, NaN, NaN, 'b--');
handles.y_line = yline(handles.axes1,0.2,'--r','Test Line');
% Update handles structure
guidata(hObject, handles);
% call the update functions to make the lines' initial state (visibility,
% ydata) consistent with the default GUI selections
update_plot(handles)
y_intercept1_Callback(handles.y_intercept1,[],handles)
% --- 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)
varargout{1} = handles.output;
function update_plot(handles)
time= 0:1/500:2;
A = get(handles.horiz_slider, 'Value');
F = get(handles.vertical_slider, 'Value');
MySignal_1 = A*sin(2*pi*F*time);
MySignal_2 = A*cos(2*pi*F*time);
set(handles.sine_line,'XData',time,'YData',MySignal_1);
set(handles.cos_line, 'XData',time,'Ydata',MySignal_2);
% --- 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
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)
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
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)
if get(hObject,'Value')
set(handles.y_line,'Visible','on');
else
set(handles.y_line,'Visible','off');
end
% --- Executes on button press in plot_cosine.
function plot_cosine_Callback(hObject, eventdata, handles)
% hObject handle to plot_cosine (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 plot_cosine
Matlab does not like the use of NaN when I have two funnctions. Any ideas?
The syntax for calling the line function is different than calling the plot function. Specifically LineSpec arguments are only for plot, so in line you have to specify the line properties with property/value pairs of arguments:
handles.cos_line = line(handles.axes1, NaN, NaN, 'Color','b', 'LineStyle','--');
Excellent! The more you know. That's all working fine and also in my other (much larger) project. Last one I think for now; how do I get the intercept line to move up and down with a slider? The following returns the default value of the slider (set to 0), but when you move the slider, nothing happens:
% create the lines. NaN is used to create a Not-a-Number array,
% which returns NaN rather than garbage if an x or y value does not exist:
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','--');
value_of_intercept = get(handles.intercept_slider, 'Value');
handles.y_line = yline(handles.axes1,value_of_intercept,'--r','Test Line');
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.
@Voss excellent. Thanks for your patience and the link above. This is quite daunting for a beginner but I am learning a lot.

Sign in to comment.

More Answers (0)

Asked:

on 25 Mar 2024

Commented:

on 27 Mar 2024

Community Treasure Hunt

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

Start Hunting!