how to avoid copying script with multiple sliders in GUI

Hi there! Very new user to Matlab here. I have created a very simple Siine plot with two control sliders. In order to get the sliders to work, I have to copy and paste the code into each slider callback:
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
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);
I have tried to use a function and recall the code for the vertical_slider but cannot figure out how to do it, so I have to paste the same stuff in:
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
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);
Very grateful of an easy peasy example to follow please! Cheers all!

11 Comments

You can avoid duplicating code by creating a separate function that updates the plot. This function can be called from both slider callbacks.
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);
end
function horiz_slider_Callback(hObject, eventdata, handles)
update_plot(handles);
end
function vertical_slider_Callback(hObject, eventdata, handles)
update_plot(handles);
end
Hi Manikanta Aditya
Thank you very much! I will try this first thing on Monday and let you know how I get on. Cheers!
Another option would be to make both sliders have the same callback function.
Hi Manikanta
I have put the function in as described and now have the following error:
>> gui_experiment_2sliders1plot_v2
Error: File: gui_experiment_2sliders1plot_v2.m Line: 129 Column: 1
The function "vertical_slider_Callback" was closed with an 'end', but at least one other function
definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions
in the same file.
I have tried to put 'end' in various polaces to close the functions, but I really do not know what I am doing as most of the code is populated by guide. I understand what it is doing, just not how to get it to work. Here is the full code, apologies it is a bit long!
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 24-Mar-2024 08:49:37
% 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;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_experiment_2sliders1plot wait for user response (see UIRESUME)
% uiwait(handles.figure1);
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);
end
% --- 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);
end
% --- 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);
end
% --- 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
@Voss thanks very much. Please could you provide me with an example?
Remove the "end" from both slider callback functions and from update_plot.
Just tried that as you typed haha! The red error bars have gone but now I get this:
Error in gui_mainfcn>local_openfig (line 286)
gui_hFigure = matlab.hg.internal.openfigLegacy(name, singleton, visible);
Error in gui_mainfcn (line 158)
gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt, gui_Visible);
Error in gui_experiment_2sliders1plot_v2 (line 42)
gui_mainfcn(gui_State, varargin{:});
An example of both sliders having the same callback would be: edit the sliders' callbacks in GUIDE - set them both to the same function; then define that function in your m-file.
Regarding the current error, can you upload both the m-file and the fig-file here using the paperclip button?
I think I have sorted it... I did not save the MATLAB .fig file so it would not run. I properly check tomorrow but just in case, here are the .m and .fig files. Thankyou very much for your help
I will experiment with the callbacks tomorrow.
@Manikanta thanks very much for your help
These run fine for me, so I think you have it sorted.

Sign in to comment.

Answers (0)

Asked:

on 24 Mar 2024

Commented:

on 25 Mar 2024

Community Treasure Hunt

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

Start Hunting!