Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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#
• 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.
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.
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 the
String property to Close and the Tag property to Close. For the axis, set the Tag property to
Axes. 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 GUI
M-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 to
daq2axis('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
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.
daq_object = analoginput('winsound');
chan = addchannel(daq_object,[1 2]);
set(daq_object,'SamplesPerTrigger',inf,'SamplesAcquiredFcnCount',num_samples,...
'SamplesAcquiredFcn',{@update_plot,handles});
We can use DAQ callbacks to specify a function called update_plot, which will update the data in
the 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
You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figure
below.
• 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 in
Phase 3 and Phase 4. We will create a GUI that will plot data from each channel to its own axis
as it is being acquired. We will also set the sample rate and the duration of the acquisition
session.
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 mouse
and then clicking and dragging a corner. Try using the Align Objects tool to vertically align the buttons.
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 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 Property
Inspector 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 (left
channel). 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 property
to 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 the
String 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 text
fields 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.
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 to
daq2axisfield('stop_Callback',gcbo,[],guidata(gcbo)). If you had already saved
your 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:
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 the
board are (e.g., daqhwinfo(daq_object)). DAQHWINFO can be used to return information (such as
minimum 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) to
update 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 execute
when 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
You can now save the GUI M-file. When you launch the GUI now, it should look similar to the figure
below.
• 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 the
duration 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.
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 no
component 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 the
Callback property to %automatic.
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 its
properties. 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 this
point, your menu should look similar to the following.
Save this GUI as daq2axisfieldplay. This step should bring up the GUI's M-file counterpart, which we
will 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 this
instance, 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:
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 stream
data 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 a
start_daq_out and stop_daq_out function for the output DAQ object daq_object_out.
There is no separate start and stop callback in this case because we used a toggle button. Starting and
stopping 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).
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.
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.
• 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 the
duration 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 GUI
uicontrols (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. Resize
the new axes component by selecting it with the mouse and then clicking and dragging a corner.
Double-click on the GUI background to bring up the Property Inspector of the GUI figure and set the
Name 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.
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 say
send_to. You can also check the Separator above this item option to get a horizontal line
separator in the final menu.
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, which
we 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
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.
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 ANALOGOUTPUT
DAQ 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:
©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 21
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 the
SamplesOutputFcn callback is run.
The duration, sample rate, and close callbacks should be unchanged from Phase 3.
The export functionality can allow the user to continue processing data immediately after the data
acquisition 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 can
also 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:
[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,:);
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 configured
correctly 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 a
relatively short amount of time. The level of complexity of your GUI will depend upon your application
needs.
©COPYRIGHT 2003 The MathWorks, Inc. All Rights Reserved. 23