Plot two figures using GUI

2 views (last 30 days)
Rihanna Waterson
Rihanna Waterson on 25 Jun 2015
Answered: Geoff Hayes on 27 Jun 2015
I try to create a GUI for my program using this link I have two figures created like this:
y1 = y1(:,1);
dt = 1/Fs1;
t = 0:dt:(length(y1)*dt)-dt;
plot(t,y1); xlabel('Seconds'); ylabel('Amplitude');
y2 = y2(:,1);
dt = 1/Fs2;
t = 0:dt:(length(y2)*dt)-dt;
plot(t,y2); xlabel('Seconds'); ylabel('Amplitude');
I inserted two push buttons. Button 1 for creating first figure and button 2 for creating the second one. How can I do that?

Answers (1)

Geoff Hayes
Geoff Hayes on 27 Jun 2015
Rihanna - you would need to put the code to build each figure within the callback for each of your push buttons. Also, you would need to somehow access the y1, y2, Fs1, and Fs2 variables. Where are you populating these variables - from a file or are they entered by the user (through the GUI or some other means)?
If y1, y2, Fs1, and Fs2 are known when you launch the GUI, you could do something like the following in the OpeningFcn
% --- Executes just before simple_gui is made visible.
function simple_gui_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 simple_gui (see VARARGIN)
% Choose default command line output for simple_gui
handles.output = hObject;
% load the y1 and y2 data
y1 = ...;
y2 = ...;
% set the sample rates
Fs1 = ...;
Fs2 = ...;
% now save these variables to the handles structure so that all callbacks
% have access to them
handles.y1 = y1;
handles.y2 = y2;
handles.Fs1 = Fs1;
handles.Fs2 = Fs2;
% Update handles structure
guidata(hObject, handles);
Note the order in the above code - we initialize the variables, set the fields of handles with these new variables, and then update the handles structure using guidata (this order is important).
Now in your pushbutton1 callback, you could do something like
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
y1 = handles.y1(:,1);
dt = 1/handles.Fs1;
t = 0:dt:(length(y1)*dt)-dt;
plot(t,y1); xlabel('Seconds'); ylabel('Amplitude');
Note that the above will create a new figure with the data that you have plotted. (If your GUI has axes controls that you wish to use to plot the data on, then you can pass the handle to the axes (obtained from handles) to the plot function. See plot(ax,...) for details.)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!