gui building

23
 ©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 1 GUI Building for Test & Measurement Applications by: Ahmed Abdalla, The MathWorks This article demonstrates how you can utilize the below-listed products to create a custom test and measurement GUI application that acquires and displays data from a PC sound card. These examples require the use of a Windows-based PC, as well as MATLAB 6.5 (R13) and the Data Acquisition Toolbox 2.2.  GUIDE (Graphical User Interface Development Environment) — a built-in drag-and-drop interface for constructing GUIs  The Data Acquisition Toolbox — a product that allows the user to interface MATLAB with many NI-DAQ, MCC, and other boards The examples in this article assume that you have familiarity with GUIDE and have read the ”Creating a GUI” example in the GUIDE documentation at the following URL: http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/simple_g.shtml  The examples also assume that you are familiar with test and measurement concepts and have gone through the ”Getting Started with the Data Acquisition Toolbox” section at the following URL: http://www.mathworks.com/access/helpdesk/help/toolbox/daq/c2_getst.shtml#10972  This article will describe phases of an application. Each successive phase is more complex than the previous and describes the functions used to create the example. The source code for the examples is available in MATLAB Central at: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=2549&objectType=file#  Phase 1: Displaying Acquired Data to an Axis  This example is intended to familiarize you with GUI creation and the data acquisition session by creating a simple GUI whose only purpose is to plot data as it is being acquired. Open up a new GUI in the Layou t Editor and add components  Open a new GUI in GUIDE and specify the size of the GUI by resizing the grid area in the Layout Editor. Click on the lower right-hand corner and resize the grid until it is approximately 3 x-4 inches or how you see fit. Add three push buttons and an axes. Arrange them as shown in the following figure. Resize the axes component by selecting it with the mouse and then clicking and dragging a corner. Try using the Align Objects tool to vertically align the buttons.

Upload: nguyen1192

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 1/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 1

GUI Building for Test & Measurement Applications 

by: Ahmed Abdalla, The MathWorks

This article demonstrates how you can utilize the below-listed products to create a custom test and

measurement GUI application that acquires and displays data from a PC sound card. These examplesrequire the use of a Windows-based PC, as well as MATLAB 6.5 (R13) and the Data Acquisition

Toolbox 2.2.

•  GUIDE (Graphical User Interface Development Environment) — a built-in drag-and-drop

interface for constructing GUIs

•  The Data Acquisition Toolbox — a product that allows the user to interface MATLAB withmany NI-DAQ, MCC, and other boards

The examples in this article assume that you have familiarity with GUIDE and have read the ”Creating aGUI” example in the GUIDE documentation at the following URL:

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_guis/simple_g.shtml  

The examples also assume that you are familiar with test and measurement concepts and have gone

through the ”Getting Started with the Data Acquisition Toolbox” section at the following URL:

http://www.mathworks.com/access/helpdesk/help/toolbox/daq/c2_getst.shtml#10972  

This article will describe phases of an application. Each successive phase is more complex than theprevious and describes the functions used to create the example. The source code for the examples is

available in MATLAB Central at:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=2549&objectType=file#  

Phase 1: Displaying Acquired Data to an Axis 

•  This example is intended to familiarize you with GUI creation and the data acquisition sessionby creating a simple GUI whose only purpose is to plot data as it is being acquired.

Open up a new GUI in the Layout Editor and add components Open a new GUI in GUIDE and specify the size of the GUI by resizing the grid area in the Layout

Editor. Click on the lower right-hand corner and resize the grid until it is approximately 3 x-4 inches orhow you see fit.

Add three push buttons and an axes. Arrange them as shown in the following figure. Resize the axes

component by selecting it with the mouse and then clicking and dragging a corner. Try using the AlignObjects tool to vertically align the buttons.

Page 2: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 2/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 2

 

Set the general properties for the GUI components You can use the Property Inspector to set the properties of each GUI component. To open the Property

Inspector, select Property Inspector from the View menu. When a component is selected in the Layout

Editor, the Property Inspector is automatically updated to display the selected component. If nocomponent is selected, the Property Inspector displays the properties of the GUI figure. Select the top

Push Button component and view its properties as shown in the following figure.

Page 3: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 3/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 3

 

Notice that the Callback property is set to %automatic. This means that GUIDE automaticallygenerates an empty callback (or framework for a callback) when the GUI is saved. We will add code to

the Callback later.

For the top button, set the String property to Start and the Tag property to Start. For the middle

button, set the String property to Stop and the Tag property to Stop. For the bottom button, set theString property to Close and the Tag property to Close. For the axis, set the Tag property toAxes. Double-click on the GUI background to bring up the properties for the figure.Set the

DoubleBuffer property to On and the Name property to Phase1: daq2axis. 

Save this GUI as daq2axis. This step should bring up the GUI M-file, which we will edit later. The GUIM-file contains all the callbacks that are used by our application when it performs an action. Notice that

if you take a look at the Callback property for any of your buttons, it is no longer %automatic.

Looking at the Stop button, we should see something similar todaq2axis('stop_Callback',gcbo,[],guidata(gcbo). If you had already saved your

GUI previous to this step then you may see something different because the callback name that is

Page 4: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 4/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 4

generated depends on the Tag that is used for that particular component. Change the Callback 

property back to %automatic and resave your GUI.

Thinking ahead, we know that we want the GUI to close in the same fashion whether we click on the1 

Close button or the X button in upper right-hand corner of the title bar. With this in mind, copy the

Close button Callback property and paste it in the CloseRequestFcn of the figure's properties.

If you click on the Run button in GUIDE (green triangle), you should see something similar to:

Note that the buttons do not do anything yet because we haven't written any code for their callbacks.

Edit the GUI M-file At this point, we need to define what actions the GUI will perform by programming the GUI M-file. For

this simple case, we can do most of the coding in the daq2axis_OpeningFcn(hObject,

eventdata, handles, varargin). Since the code in this function is the first to run once theGUI is created, we can initialize the GUI elements (such as axis) and create the data acquisition

ANALOGINPUT object to read from your PC's sound card here. The ANALOGINPUT object is created

and the channels are added with the following code:

daq_object = analoginput('winsound');

chan = addchannel(daq_object,[1 2]);

We can use the SamplesAcquiredFcn function of an ANALOGINPUT object to execute a series of 

commands every time a certain number of samples are acquired. A line similar to the following shouldbe used after creating the DAQ object so that the plot is continuously updated after a specified number

of samples, num_samples, are collected:

set(daq_object,'SamplesPerTrigger',inf,'SamplesAcquiredFcnCount',num_samples,...

'SamplesAcquiredFcn',{@update_plot,handles});

Page 5: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 5/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 5

After we create the data acquisition object, it needs to be accessible to the rest of the functions in the

GUI. In order to accomplish this, we can add the daq_object variable to the Handles structure. Formore information on the Handles structure and sharing information in your GUI, please see the Sharing

Data Between Callbacks section of the GUIDE documentation.

We can use DAQ callbacks to specify a function called update_plot, which will update the data inthe axes. We will use Handle Graphics (the SET command in particular) to update the YData property

of the plot. The code used in this callback is:

data = getdata(handles.daq_object,handles.num_samples);

for i = 1:length(handles.plot_handle)set(handles.plot_handle(i),'YData',data(:,i));

end

The start_Callback(hObject, eventdata, handles),

stop_Callback(hObject, eventdata, handles) and close_Callback(hObject,

eventdata, handles) callbacks are all very small. Each of the callbacks in this case has a veryspecific function, which is to start acquisition using the Start command, stop acquisition with the Stop

command, and close the GUI with the Close command, respectively.

You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figure

below.

Run the GUI Run the GUI by either pressing the Run button (green triangle) in the GUIDE window or typing the

name of the GUI at the MATLAB command prompt. Press Start and watch the signal that is beingacquired on your sound card (signal from a CD, MP3, etc.).

Page 6: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 6/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 6

 

Phase 2: Displaying Data to Axes and Acquiring User Input 

•  This phase shows how to control some parameters of the data acquisition session. In this phase,

we will begin developing a more sophisticated application whose functionality will grow inPhase 3 and Phase 4. We will create a GUI that will plot data from each channel to its own axisas it is being acquired. We will also set the sample rate and the duration of the acquisition

session.

Open up a new GUI in the Layout Editor and add components

Open a new GUI in GUIDE and specify the size of the GUI by resizing the grid area in the Layout

Editor.

Add 7 static text fields, 2 text edit fields, 2 axes, and 3 push buttons and arrange them in a similar

manner to that shown in the following figure. Resize the axes component by selecting it with the mouseand then clicking and dragging a corner. Try using the Align Objects tool to vertically align the buttons.

Page 7: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 7/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 7

You can now add a frame to your GUI. Resize and position the frame so that it covers the 7 static text

fields and the 2 text edit fields.

Set the general properties for the GUI components To view the 7 static text fields and the 2 text edit fields again, right-click on the frame and choose Send

to Back.

To set the properties of each GUI component, select the Property Inspector from the View menu todisplay the Property Inspector. When you select a component in the Layout Editor, the Property

Inspector displays the component's properties. If no component is selected, the Property Inspector

displays the properties of the GUI figure. Double-click on the GUI background to bring up the PropertyInspector of the GUI figure. Set the Name field to Phase 2: daq2axisfield and set the

DoubleBuffer property to On. 

Double-click on the top axis to view its properties and change the Tag property to axesL (leftchannel). Double-click on the bottom axis to view its properties and change the Tag property to axesR

(right channel).Double-click on the left most push button and set the String property to Start and the Tag propertyto Start. For the button to the left, set the String property to Stop and the Tag property to Stop.

For the last button, set the String property to Close and the Tag property to Close.

Double-click on the top text edit field and change the Tag property to sample_rate. Change the Tag 

property of the text edit field below that to duration.

Double-click on the top most static text field (this field will be the label of the frame) and set theString property to Modifiable Parameters. Working your way down from there, set the String

property of the next 4 static text fields to the following respectively: Sample Rate, Current

Sample Rate (Hz), Duration, Current Duration (sec). You can resize the textfields so that all strings display correctly. For the remaining 2 text fields, set the String property to ''

(empty) and set the Tag properties to current_Fs and current_dur respectively. Your GUI

should now look similar to the following figure.

Page 8: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 8/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 8

 

Notice that there are 2 invisible text fields (highlighted in the figure above) that are there but are notvisible. We will use these later to show what the currently stored sample rate and duration values are.

Save this GUI as daq2axisfield. This step should bring up the GUI's M-file counterpart, which we

will edit later on in this phase so that our application performs an action. Notice that if you look at the

Callback property for any of your buttons, it is no longer %automatic. Looking at the Stop button,we should see something similar todaq2axisfield('stop_Callback',gcbo,[],guidata(gcbo)). If you had already savedyour GUI previous to this step then you may see something different. Change the Callback property

back to %automatic and resave your GUI.

Thinking ahead, we know that we want the GUI to close in the same fashion whether it is the Close

button that is clicked or the X button in the upper right-hand corner of the title bar. With this in mind,

copy the Close button Callback property and paste it in the CloseRequestFcn of the figure's

properties.

If you click on the Run button in GUIDE (green triangle), you should see something similar to:

Page 9: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 9/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 9

 

Note that the buttons do not do anything yet because we haven't written any code for their callbacks.

Edit the GUI M-file At this point, we will define what actions the GUI will perform by programming the GUIDE-generatedM-file. For this case, we can use the daq2axisfield_OpeningFcn(hObject, eventdata,

handles, varargin) callback to initialize the GUI elements (such as axis and text fields) and

create the data acquisition ANALOGINPUT object to read from your PC's sound card.

Thinking ahead to the error checking in the callback for the sample rate input field, we can also use the

DAQHWINFO command to find out what the minimum and maximum sample rates supported by theboard are (e.g., daqhwinfo(daq_object)). DAQHWINFO can be used to return information (such asminimum and maximum sample rate capabilities) about the data acquisition board that is being used. As

in Phase 1, a line similar to the following should be used after creating the DAQ object so that the plot is

continuously updated after a specified number of samples, num_samples, are collected:

set(daq_object,'SamplesPerTrigger',inf,'SamplesAcquiredFcnCount',num_samples,...'SamplesAcquiredFcn',{@update_plot,handles}, 'StopFcn',{@stop_daq,handles},

'StartFcn',{@start_daq,handles});

We can use DAQ callbacks to specify a function called update_plot that will update the data in the

axes. The heart of this function will be the use of Handle Graphics (the SET command in particular) toupdate the YData property of the plot. We can also use DAQ callbacks to specify the function

start_daq for the StartFcn property and stop_daq for the StopFcn property, which will executewhen acquisition starts and stops respectively. In this case, we can just use these callback functions to

enable/disable GUI elements depending on the Running status of the DAQ object.

Since we are allowing the user to enter the sample rate, there will be some error checking involved in the

sample_rate_Callback(hObject, eventdata, handles) function. We can make sure

Page 10: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 10/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 10

that the specified sample rate is within the minimum and maximum bounds for the card being used

(remember that we obtained this information in the OpeningFcn). A good function to use in this callback is SETVERIFY. This function will set the DAQ object sample rate to the specified value and will return

the true sample rate as seen by the object since sound cards generally only support a support a subset of 

sample rates.

Error checking for the duration parameter can be performed in duration_Callback(hObject,

eventdata, handles). Here, check to make sure that the duration specified is either a number orInf.

The start_Callback(hObject, eventdata, handles),

stop_Callback(hObject, eventdata, handles), and

close_Callback(hObject, eventdata, handles) callbacks are all very short. Each of the

callbacks in this case has a very specific function: to start acquisition, stop acquisition, and close theGUI, respectively.

You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figurebelow.

Run the GUI Run the GUI by either pressing the Run button (green triangle) in the GUIDE window or typing the

name of the GUI at the MATLAB command prompt. Press Start and watch the signal that is beingacquired on your sound card (signal from a CD, MP3, etc.). Try changing the duration and sample rate

parameters during your session.

Page 11: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 11/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 11

Phase 3: Displaying, Saving, and Playing Data and Acquiring User Input

•  We will build upon the application developed in Phase 2 to create a GUI that will plot data from

each channel to its own axis as it is being acquired, allow the user to set the sample rate and theduration of the acquisition session, and also allow the user to save and play back the acquired

data.

Open up the Phase 2 GUI in the Layout Editor and add/edit components 

Open the Phase 2 GUI in GUIDE by choosing File -> Open, and then selecting the GUI’s name.

Leave the static text fields, text edit fields, and axes unchanged. Replace the Start push button with a

toggle button and leave the other 2 push buttons unchanged.

Page 12: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 12/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 12

 

Set the general properties for the GUI components The properties of each GUI component should be left unchanged except for the toggle button and theStop button.

Select the Property Inspector from the View menu to display the Property Inspector. When you select a

component in the Layout Editor, the Property Inspector displays the component's properties. If nocomponent is selected, the Property Inspector displays the properties of the GUI figure. Double-click on

the GUI background to bring up the property inspector of the GUI figure and set the Name field to

Phase 3: daq2axisfieldplay and set the DoubleBuffer property to On.

Double-click on the toggle button and set the String property to Start and the Tag property to

start_stop.

Double-click on the currently labeled Stop push button and set the String property to Play and the

Tag property to Play. You will need to regenerate the push button’s callback stub. To do so, set theCallback property to %automatic.

Your GUI should now look similar to the following figure.

Page 13: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 13/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 13

 

Notice that similar to Phase 2, there are 2 invisible text fields.

Now let us set up the Menu. Click on Tools -> Menu Editor to bring up the Menu Editor GUI. Click on

the New Menu icon to add a new top-level menu. Highlight the new Untitled 1 menu item to view itsproperties. Edit the label to say File and the tag to say File. Click on the New Menu Item icon to add a

new item to our top level File menu. Highlight the Untitled 2 menu item to view its properties. At thispoint, your menu should look similar to the following.

Page 14: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 14/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 14

 

Edit the label for this menu item to say Save and the tag to Save. Highlight the File menu item and add

another menu item under it by clicking the New Menu Item icon. Highlight this Untitled 3 menu itemand change the label to say Close and the tag to say Close. You can also check the Separator above thisitem option to get a horizontal line separator in the final menu.

Save this GUI as daq2axisfieldplay. This step should bring up the GUI's M-file counterpart, which wewill edit later so that our application performs an action. Notice that if you look at the Callback 

property for any of your buttons, it is no longer %automatic. Looking at the Stop button, we should

see something similar to daq2axisfield('stop_Callback',gcbo,[],guidata(gcbo)). 

If you had already saved your GUI previous to this step then you may see something different. In thisinstance, change the Callback property back to %automatic and resave your GUI.

Thinking ahead, we know that we want the GUI to close in the same fashion whether it is the Closebutton that is clicked or the X button in the upper right-hand corner of the title bar. With this in mind,

copy the Close button Callback property and paste it in the CloseRequestFcn of the figure's

properties.

If you click on the Run button in GUIDE (green triangle), you should see something similar to:

Page 15: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 15/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 15

 

Note that the buttons do not do anything yet because we haven't written any code for their callbacks.

Edit the GUI M-file At this point, we will define what actions the GUI will perform by programming the GUIDE generated

M-file. We can use the daq2axisfieldplay_OpeningFcn(hObject, eventdata,

handles, varargin) callback to initialize the GUI elements (such as axis and text fields) and

create the data acquisition ANALOGINPUT object to read from your PC's sound card. We will leave

this callback mostly unchanged from Phase 2 except that a line similar to the following should be used

after creation of the ANALOGINPUT object:

set(daq_object,'LoggingMode','disk','LogFileName','untitled.daq',...'StartFcn',{@start_daq,handles},'StopFcn',{@stop_daq,handles});

Notice that the logging mode of the data acquisition object is now different. We are choosing to streamdata directly to the hard disk in a file called untitled.daq. This means that we can avoid worrying about

the buffer size for large acquisitions and we can access the acquired data after the session is over.

We can also create an ANALOGOUTPUT object to play back the acquired waveform to your PC's

sound card:

set(daq_object_out,'StartFcn',{@start_daq_out,handles},'StopFcn',{@stop_daq_out,handl

es});

We are using the same callback strategy that we have already used in Phase 2 to set astart_daq_out and stop_daq_out function for the output DAQ object daq_object_out. 

Page 16: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 16/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 16

The sample rate and duration callbacks should be left unchanged from Phase 2.

When the user calls play_Callback(hObject, eventdata, handles) by clicking on the

Play button, we can start the ANALOGOUTPUT DAQ object here. If there is no data to play back (i.e.,

none has been acquired), we can return a meaningful message to let the user know that data must be

acquired first before playing.

The save functionality can be implemented in save_Callback(hObject, eventdata,

handles) which is executed when the user chooses the Save option from the File menu. You can add

the ability to save .daq files by renaming the acquired .daq file. The ability to save .mat files can be

added by using daqread to read in the acquired data and then using the SAVE command to save thedata as a .mat file:

[data time] = daqread('untitled.daq');save([path filename '.mat'],'data','time');

There is no separate start and stop callback in this case because we used a toggle button. Starting andstopping ANALOGINPUT acquisition will be similar to the previous phases, but all the code will be

contained in a single start_stop_Callback(hObject, eventdata, handles). 

The close_Callback(hObject, eventdata, handles) callback is more complicated in

this phase because you need to make sure that both the input and output DAQ objects are not running

and have been deleted before exiting the GUI:

if(strcmp(handles.daq_object.running,'On'))

stop(handles.daq_object);end

if(strcmp(handles.daq_object_out.running,'On'))stop(handles.daq_object_out);end

delete(handles.daq_object);

delete(handles.daq_object_out);

clear handles.daq_object handles.daq_object_out;

if exist('untitled.daq')

delete untitled.daq;end

delete(handles.figure1)

You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figure

below.

Page 17: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 17/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 17

 

Run the GUI Run the GUI by either pressing the Run button (green triangle) in the GUIDE window or typing thename of the GUI at the MATLAB command prompt.

Press Start and watch the signal that is being acquired on your sound card (signal from a CD, MP3,etc.). Try changing the Duration and Sample Rate parameters during your session. Press Stop to stop

acquiring data and then press Play to listen to your signal. Try saving the acquired signal as a .daq or a

.mat file.

Page 18: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 18/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 18

Phase 4: Displaying, Saving, and Playing Data; Acquiring User Input, and More

•  We will build upon the application developed in Phase 3 to create a GUI that will plot data from

each channel to its own axis as it is being acquired, allow the user to set the sample rate and theduration of the acquisition session, and allow the user to save, export, play back, and pan through

the acquired data. This GUI will also provide an example of how to use images on your GUIuicontrols (push/toggle buttons).

Open up the Phase 3 GUI in the Layout Editor and add/edit components 

Open the Phase 3 GUI in GUIDE by choosing File -> Open and then selecting the GUI’s name.

Leave the static text fields, text edit fields, axes, toggle button, and push buttons unchanged. Add one

axes and one slider and arrange them in a similar manner to that shown in the following figure. Resizethe new axes component by selecting it with the mouse and then clicking and dragging a corner.

Set the general properties for the GUI components To set the properties of each GUI component, select the Property Inspector from the View menu to

display the Property Inspector. When you select a component in the Layout Editor, the PropertyInspector displays the component's properties. If no component is selected, the Property Inspector

Page 19: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 19/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 19

displays the properties of the GUI figure. All of the elements from Phase 3 will remain unchanged but

the new elements will need some edits.

Double-click on the GUI background to bring up the Property Inspector of the GUI figure and set theName field to Phase 4:SoundRecorder Demo and set the DoubleBuffer property to On.

Double-click on the bottom axis and change the Tag property to sig.

Double-click on the slider and set the Tag to chan1slider.

Your GUI should now look similar to the following figure.

Now we will edit the Menu. Click on Tools -> Menu Editor to bring up the Menu Editor GUI. You

should already see a top-level menu called File with two menu items that we created in Phase 3: Save As

and Close.

Highlight the File menu item and add another menu item under it by clicking the New Menu Item icon.

Highlight this Untitled 3 menu item and change the label to say Send To and the tag to saysend_to. You can also check the Separator above this item option to get a horizontal lineseparator in the final menu.

Page 20: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 20/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 20

Highlight the Send To menu item and add another menu item under it by clicking the New Menu

Item icon. Highlight this Untitled 5 menu item and change the label to say Workspace and thetag to say send_to_workspace. Add two more menu items on the same level as Workspace with their

labels set to Figure and SPTool and their tag set to send_to_figure and send_to_sptool,

respectively.

Add a new top-level menu by clicking the New Menu icon. Set the label to Help and the tag to Help.

Add three new menu items to this Help menu by using the New Menu Item icon. Set the labels of these to SoundRecorderDemo Information, Data Acqusition Toolbox, and More

Demos... and set the tags to SRD_help, daq_help, and demos_help, respectively. Your

menu tree should now look like the following:

Save this GUI as SoundRecorderDemo. This step should bring up the GUI's M-file counterpart, whichwe will edit later so that our application performs an action. If you had already saved your GUI previous

to this step then change the Callback properties back to %automatic and resave your GUI so that

appropriately named callback functions are generated.

Thinking ahead, we know that we want the GUI to close in the same fashion whether it is the Close

menu item that is selected or the X button in the upper right-hand corner of the title bar. With this in

Page 21: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 21/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 21

mind, copy the Close menu item Callback property and paste it in the CloseRequestFcn of the

figure's properties.

If you click on the Run button in GUIDE (green triangle), you should see something similar to:

Note that the buttons do not do anything yet because we haven't written any code for their callbacks.

Edit the GUI M-file At this point, we will define what actions the GUI will perform by programming the GUIDE generated

M-file. We can use the same OpeningFcn callback to initialize the GUI elements (such as axis and

text fields). We can create the data acquisition ANALOGINPUT object to read from your PC's soundcard and an ANALOGOUTPUT object to play back to your PC's sound card. This callback will remain

unchanged from Phase 3 except that we will add some extra code to initialize the added GUI elements.

Use a line similar to the following after creating your DAQ input object:

set(handles.daq_object,'LoggingMode','disk','LogFileName',[handles.fname '.daq'],...

'StartFcn',{@start_daq,handles},'StopFcn',{@stop_daq,handles});

Note that the data logging method is unchanged from Phase 3. We will also want to use the same

callback strategy to set a start_daq_out and stop_daq_out function for the ANALOGOUTPUTDAQ object daq_object_out. We can use a line such as the following so that we know how many

samples are output to the soundcard. This will enable us to update our GUI to indicate that playback is

progressing:

Page 22: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 22/23

 

©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 22

set(handles.daq_object_out,'SampleRate',fs,'SamplesOutputFcnCount',preview,'Samples

OutputFcn',{@selectRegion,handles,2});

The selectRegion function is a custom function that will update GUI elements every time theSamplesOutputFcn callback is run.

The duration, sample rate, and close callbacks should be unchanged from Phase 3.

The slider callback function, chan1slider_Callback(hObject, eventdata, handles), 

will be more involved to create. In this callback, the slider will have to update all the axes appropriately

to show the full signal and the local (zoomed-in) signal. This can be accomplished using the SETcommand to set the plot XData and YData properties.

When the user calls play_Callback(hObject, eventdata, handles) by clicking on thePlay button, we can start the ANALOGOUTPUT DAQ object here. If there is no data to acquire, let the

user know.

The save functionality already implemented in save_as_Callback(hObject, eventdata,

handles) can be left unchanged but we can add the ability to save .wav files by using WAVWRITE.

The export functionality can allow the user to continue processing data immediately after the dataacquisition session has ended. You can use the EVALIN command to send the acquired data to the

workspace. You can use the PLOT and SUBPLOT commands to send the data to a figure and the

SPTOOL command to send the data to SPTOOL if it is installed.

If you would like existing MATLAB documentation to be displayed from the Help menu, use the DOC

command. If you would like to point to your own HTML documentation, use the WEB command.

There are no separate Start and Stop callbacks in this case because we used a toggle button so that

the example would very similar to the callback from Phase 3. However, we also need to add the

functionality of loading the acquired data into the GUI to populate the full signal axes. We canalso make the call to an update_plot function, which does the work of updating the current signal

axes with data as it is being acquired.

The graphics on the push buttons can be implemented by writing a simple iconize function, which

subsamples the image to fit a particular size. For example the iconize function may look like:

function out = iconize(a)

% 'a' is an image that is read into MATLAB using IMREAD

[r,c,d] = size(a);

r_skip = ceil(r/18);c_skip = ceil(c/18);

out = a(1:r_skip:end,1:c_skip:end,:);

Page 23: Gui Building

8/6/2019 Gui Building

http://slidepdf.com/reader/full/gui-building 23/23

 

©COPYRIGHT 2003 The MathWorks Inc All Rights Reserved 23

 

You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figurebelow.

Run the GUI Run the GUI by either pressing the Run button (green triangle) in the GUIDE window or typing the

name of the GUI at the MATLAB command prompt.

Press Start and watch the signal that is being acquired on your sound card (signal from a CD, MP3, etc.).

Try changing the Duration and Sample Rate parameters during your session. Press Stop to stop

acquiring data and the press Play to listen to your signal. Try saving the acquired signal as a .daq, .mat,or .wav file. Try exporting the acquired signal to the workspace or another figure.

If you have problems seeing the signal at this point, make sure that you have Windows configuredcorrectly so that you are recording from the appropriate source. For more information on this issue,

please see the Data Acquisition Toolbox documentation at the following URL:

http://www.mathworks.com/access/helpdesk/help/toolbox/daq/a1_hard8.shtml#9081  

Conclusion Using existing MATLAB tools such as GUIDE and Handle Graphics along with the Data Acquisition

Toolbox can enable you to develop aesthetically appealing and fully functional GUI applications in arelatively short amount of time. The level of complexity of your GUI will depend upon your application

needs.