MATLAB Interface

From BESA® Wiki
Jump to: navigation, search
Module information
Modules BESA Research Basic or higher
Version 6.1 or higher

MATLAB Interface

Introduction

BESA Research has menu items "Send to MATLAB..." at various locations that allow to send data as structures to Matlab. After sending the data, BESA Research starts a Matlab script that can be used to start further data analysis on the data structure. These scripts, located in the Scripts\Matlab folder in the BESA Research installation, can be edited by the user to perform further data analysis in Matlab. For example, if "Send to MATLAB" is selected in the File Export Dialog, a data structure "besa_channels" will be created. After filling the structure, the script "besa_action_channels.m" will be started.

All the Matlab export functions are batchable. Thus, a complete data analysis can be performed, in which BESA Research does the preprocessing, and passes the data on to Matlab for a statistical analysis of the results.

An example for the application of the MATLAB interface is demonstrated in the BESA Research Tutorial on Batch Scripts, Multiple Subjects & Conditions, MATLAB-Interface. You can download this tutorial from our website at www.besa.de/downloads/training-material/tutorials/.

Some MATLAB scripts that can be used in conjunction with the BESA Research MATLAB interface are also available on our website at www.besa.de/downloads/matlab/. You are invited to send your own scripts for data analysis or to submit any questions or feedback here: www.besa.de/support/support-page/.

Important! Please follow the instructions in the “Installation” chapter! Then read the “How the interface works” section to get started.

Configuration

In order for the BESA-Matlab interface to work, please follow the instructions below. If you start BESA Research, and the file menu does not display the "Send to MATLAB" item, the interface is not installed correctly!

1. Matlab must be installed.

For step 2 (required for MATLAB versions 2009b and over), we need to know whether the 32-bit or 64-bit version of Matlab is installed. We also need to know the path to the Matlab installation (e.g. c:\Program Files\MATLAB\2009b\).

2. PATH environment variable:

For versions 2009b and over, make sure that the path to the Win32 or Win64 folder in the Matlab installation to the PATH environment variable is defined:

  • The path for the 64-bit version for Matlab 2009b is typically c:\Program Files\MATLAB\2009b\bin\win64. For the 32-bit version, the path is typically c:\Program Files\MATLAB\2009b\bin\win32.
  • Open the "System Properties" Dialog by holding down the Windows key and pressing the Pause button. In XP, the dialog is opened directly. In Vista and Window 7, the key combination opens the System Display. Click on the link "Change Settings".


Matlab (1) .gif


  • Select the "Advanced" tab.


Matlab (2) .gif


  • Press the "Environment Variables" button.


Matlab (3) .gif


  • Under "System variables" click on the "Path" variable and press "Edit". In the resulting dialog, enter a semicolon (;) at the end of the path string, and add the path after the semicolon.


Matlab (4) .gif


  • Press "OK" to close and save the path variable. Press "OK" to close the "System Properties" Dialog.


3. Additional configuration (only required after a change of your MATLAB configuration after installation of BESA Research):

During the installation process of BESA Research, the program "SetupBesaMatlabInterface.exe" in the BESA Research root folder was executed. Run this program again when your MATLAB configuration has changed, e.g. after updating your MATLAB version.

In the dropdown list, select the Matlab version that you are using.

This program does two operations:

1. it copies the appropriate interface Dll to the BESA Research root folder and renames it to "BesaMatlab.dll" (32-bit version) or "BesaMatlab64.dll" (64-bit version), and

2. if you are using a 64-bit version it creates an entry in BESA.ini as follows:

[Matlab]

Platform=64


4. Testing:

Start BESA Research and check if Send to MATLAB is displayed in the File menu. If it is, the interface is set up correctly. (Note that the item will be grayed if no file is open in BESA Research.)

Test the interface: open a data file, mark a short (e.g. 1 s) time range, and select File / Send to Matlab to open the Export Dialog:


Matlab1.png


The Matlab window should open, and BESA Research will display a progress bar:


Matlab (6) .gif


After the window closes, open the Matlab window, and type "workspace" to open the workspace window, or "desktop" to open the standard Matlab desktop.

Examine the "besa_channels" variable, which contains the data for the marked data segment.


Matlab (7) .gif


Troubleshooting if the interface is not working after the above steps

If the File / Send to MATLAB... menu item is not shown, this means that either the path (step 2 above) is not defined properly, or that the interface Dll BesaMatlab.dll or BesaMatlab64.dll is not compatible with the currently installed version of Matlab.

If the path is correct, then please contact our support team here: http://besa.de/contact/support/form.php, including the following information:

  • Which Matlab version are you using?
  • Specify also if you are using the 32-bit or 64-bit version.

How the interface works

The interface

The interface uses libraries supplied by Matlab. Their descriptions can be found in Matlab Help under the keywords "Engine Library". The Matlab libraries are incorporated into the interface Dll (BesaMatlab.dll or BesaMatlab64.dll) that provides the interface between BESA Research and Matlab. As newer versions of Matlab are released, it may be necessary to generate new versions of the dll to match the new library versions.

Matlab automation window

On the first call to one of the Matlab routines, the Matlab Automation Window is opened. This is not the same as the window that is normally opened when Matlab is started directly in Windows (the window can also be opened by typing "Matlab /automation" from the command line). From the Automation Window one can run normal Matlab scripts. It is also possible to type "desktop" in the Automation Window to open the standard Matlab desktop. All variables that have been sent from BESA Research are then visible there.

BESA Research "Send to MATLAB" commands and scripts

Send to MATLAB commands generate data structures that differ depending on the type of data that are sent. After each export the corresponding script is executed. They are available at the following locations in BESA Research:

  • From the Main program window (File Menu), as part of the Export Dialog. Structure name besa_channels. Script name besa_action_channels.m.
  • From the FFT analysis (File Menu). Structure name besa_fft. Script name besa_action_fft.m.
  • From Combine Conditions (Run Scripts Tab), in the export of peaks and mean amplitudes. Structure name besa_peak. Script name besa_action_peak.m.
  • From Source Analysis (File Menu), for exporting source waveforms (besa_sourcewaveforms), source models (besa_sourcemodel), data, residual and model waveforms (besa_sa_channels), and 3D images (besa_image). Script names besa_action_sourcewaveforms.m, besa_action_sourcemodel.m, besa_action_sa_channels.m, and besa_action_image.m.
  • From Time-Frequency/Coherence Analysis (File Menu). Structure names besa_tfc, and besa_tfc_trials (for single-trial time-frequency data). Script names besa_action_tfc.m and besa_action_tfc_trials.m.


Some additional scripts are used for specific data types. For example, when exporting raw data, two scripts, besa_helper_channels_event.m and besa_helper_channels_continuousdata.m are used to collect events from each data block and to combine the exported data blocks into a single matrix.

Command script path

When the commands are executed, BESA Research automatically executes an "addpath" command in Matlab to add the Scripts\MATLAB folder (in Windows 7 typically C:\Users\Public\Public Documents\BESA\Research_6_0\Scripts\MATLAB) and its first-level subfolders to the Matlab search path.

Units

Unless otherwise stated, distances are in meters, times are in seconds, and the head-frame (fiducial-based) coordinate system is used.