Difference between revisions of "AnyWave:WriteMatlabScripted"

From WikiMEG
Jump to: navigation, search
(AwGetData())
(main.m)
 
(152 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
=Introduction=
 
=Introduction=
This section targets people who have a good knowledge and practice of Matlab language.<br/>
+
This section targets people who have a good knowledge and practice of the MATLAB programming language.<br/>
The purpose is to explain how to write a Matlab function that will be available to AnyWave.<br/>
+
The purpose is to explain how to write a MATLAB function that will be the heart of a plug-in executed in MATLAB by AnyWave.<br/>
The function will be executed by MATLAB after a connection to AnyWave has been established.<br/>
+
We will also explain how to create a text file to describe our plug-in to AnyWave<br/>
We will cover the AnyWave-Matlab API (Application Programming Interface) and develop examples through tutorials.<br/>
+
The AnyWave-MATLAB API consists in a set of MATLAB functions. They will be described in details in a dedicated section.<br/>
  
=Where to start?=
+
=Setup the plugin folder=
 
The first thing to do is to create the basic structure for a plug-in.<br/>
 
The first thing to do is to create the basic structure for a plug-in.<br/>
A Matlab Scripted plug-in is very simple, it is a folder containing at least two files.<br/>
+
A MATLAB Scripted plug-in is very simple: it is a folder containing at least two files.<br/>
<br/>
+
 
Let's begin by creating a folder somewhere on the computer, called '''MyPlugin'''.<br/>
+
Let's begin by creating a folder (called MyPlugin) somewhere on the computer.<br/>
 
This can be done in MATLAB: create a folder and create a new function called '''main''' in that folder.<br/>
 
This can be done in MATLAB: create a folder and create a new function called '''main''' in that folder.<br/>
 
The main function is '''MANDATORY'''. It will be the main function AnyWave will call to execute our plugin.<br/>
 
The main function is '''MANDATORY'''. It will be the main function AnyWave will call to execute our plugin.<br/>
Line 15: Line 15:
 
As shown in the image above, a MyPlugin folder has been created and a main function was added.<br/>
 
As shown in the image above, a MyPlugin folder has been created and a main function was added.<br/>
 
<br/>
 
<br/>
We are now ready to write our first Matlab plug-in but before doing that we need to cover all the MATLAB functions provided by AnyWave.<br/>
+
We are now ready to write our first Matlab plug-in!!
Indeed, to get or put data from/to AnyWave, special functions are provided.<br/>
+
We can call that set of functions the ''AnyWave-MATLAB API''.<br/>
+
  
=AnyWave-Matlab functions=
+
Refer to the [[AnyWave#AnyWave-MATLAB functions|''AnyWave-MATLAB functions'']] section to learn how to program a plug-in that will communicate with AnyWave.
The table below shows a summary of all matlab functions available when writing a Matlab Scripted plug-in:<br/>
+
 
Function parameters placed between <> are optional.<br/>
+
=Writing the desc.txt file=
 +
We have written the MATLAB code and now all we have to do is to create a descriptive text file to inform AnyWave about our plug-in.<br/>
 +
Let's dot it in Maltab:<br>
 +
[[File:Matlab2.png|center]]
 +
 
 +
The file must be named '''desc.txt''' and may looks like:
 +
<syntaxhighlight lang="text">
 +
name = My Matlab Plugin
 +
description = I am a plug-in written in Matlab
 +
category = Process:MATLAB:My MATLAB Plugin
 +
</syntaxhighlight>
 +
The syntax is to set keywords and values.<br /><br />
 +
 
 +
Here we have three keywords (name, description, category).<br />
 +
Two keywords are mandatory : name and description. Other keywords are optional.<br />
 +
==desc.txt keywords==
 +
Some keywords may have several values. Separate the values by the colon character ''':''' <br/>
 +
The table below shows all the keywords handled by AnyWave and their functions.<br/>
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! Function !! Short description
+
! keyword !! description !! type
 
|-
 
|-
| '''AwGetInputChannels'''() || Get current input channels set by AnyWave for the plug-in.
+
| name || the name of your plugin. Must be unique.|| MANDATORY
 
|-
 
|-
| '''AwGetData'''(start, duration, <filtering>, <filteringOptions>) || Get the data of the current input channels.
+
| description || short description || MANDATORY
 
|-
 
|-
| '''AwGetWorkingDir'''() || Get the full path to a temporary directory created by AnyWave for the plug-in.
+
| category || Where to link the plugin in AnyWave menus || OPTIONAL
 
|-
 
|-
| '''AwGetFilePath'''() || Get the full path of the current open data file in AnyWave.
+
| input_flags || what is required for input || OPTIONAL
 
|-
 
|-
| '''AwAddMarker'''(value, position, duration, <targeted channels>) || Add a marker in AnyWave.
+
| flags || The plugin special flags || OPTIONAL
|-
+
| '''AwSendMessage'''(message) || Send a message to AnyWave. The message will be added to the plug-in's log.
+
 
|}
 
|}
  
==AwGetInputChannels()==
+
===category===
AnyWave uses channels to represent signals on screen but also as its internal data set.<br/>
+
'''category:''' It tells AnyWave where the plug-in will appear in the menus. Here, we decided to make it appear under the Python sub-menu in the Processes main menu.<br />
When a plug-in is launched by AnyWave, a set of input and/or output parameters is defined accordingly to the specific settings for the plug-in.<br/>
+
The category feature is usefull to separate plug-ins by theme.<br/>
The default behavior is to set as input channels the current selected channels in AnyWave. If there is no selection, then AnyWave will set all the channels in the current montage as input channels.<br/>
+
Three category keywords are recognized:<br/>
<br/>
+
* Process :  The plug-in will be set in the Processes menu with a subcategory and a name, for example 'Process:Correlation:Compute correlation'
'''AwGetInputChannels()''' will request the list of input channels for the plug-in. No parameters are required.<br/>
+
* File: The plug-in will be set in the File Menu under the Export sub-menu. Example : 'File:Export to file.'
AnyWave will send back a structure array containing Matlab structures which match the structure defined by the '''AwChannel()''' function.<br/>
+
* View: The plug-in will be set in the View Menu. Example : 'View:Launch 3D viewer'
'''AwChannel()''' is provided by AnyWave to easily create a Matlab structure which will be compatible with AnyWave's channels objects.<br/>
+
If no category is specified, AnyWave will set the plug-in in the Processes menu using the name defined in the file.<br/>
 
<br/>
 
<br/>
This function is usefull if you want to get input the channels' informations without requesting for data.<br/>
 
  
Let's try it in our main function for our plug-in:<br/>
+
===input_flags===
<syntaxhighlight lang="matlab">
+
'''input_flags:''' a list a ":" separated strings that set the input flags for the plugin.<br/>
function main()
+
%MAIN Summary of this function goes here
+
%  Detailed explanation goes here
+
 
+
channels = AwGetInputChannels();
+
disp('Input channels:');
+
for i=1:numel(channels)
+
    fprintf('name: %s', channels(i).name);
+
    fprintf('ref: %s', channels(i).ref);
+
    fprintf('sampling rate: %g Hz', channels(i).samplingRate);
+
end
+
  
end
+
{| class="wikitable"
 +
|-
 +
! input_flag !! description
 +
|-
 +
| GetAllMarkers || AnyWave will copy all the markers and set them as input for the plugin. This will impact aw_getmarkers() function.
 +
|-
 +
| GetDurationMarkers || AnyWave will copy markers with a duration and set them as input. This will impact aw_getmarkers() function
 +
|-
 +
| ProcessIgnoresChannelSelection || The plugin will be run without using the selected channels as input by default.
 +
|-
 +
| ProcessRequiresChannelSelection || The plugin will only run if the user has selected channels.
 +
|-
 +
| GetAsRecordedChannels || AnyWave will copy all the as recorded channels found in the current file as input for the plugin. This will impact aw_getdata() function.
 +
|-
 +
| GetCurrentMontage || AnyWave will copy the current montaged channels and set them as input for the plugin. This will impact aw_getdata() function.
 +
|}
 +
Example of desct.txt with input_flags:<br/>
 +
<syntaxhighlight lang="text">
 +
name = MyPlugin
 +
description = do something in MATLAB
 +
category = Process:Test:MyPlugin
 +
input_flags = ProcessIgnoresChannelSelection:GetAllMarkers
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
===flags===
 +
flags are used to configure the plugin capabilities.<br/>
 +
{| class="wikitable"
 +
|-
 +
! flag !! description
 +
|-
 +
| NoDataRequired or ProcessDoesntRequireData || indicates the plugin doesn't need a file to be open in AnyWave to run.
 +
|-
 +
| CanRunFromCommandLine || indicates that the plugin can also run in batch mode and using the command line.
 +
|}
  
As explained above, the channels variable is a Matlab array of structs filled by AnyWave.<br/>
+
=main.m=
The function will then enumerate the contents of the array.<br/>
+
This is the entry point function called by AnyWave.<br/>
The structure contains 4 fields but only three are usefull in our case:<br/>
+
Depending on the type of plugin you are writing, you may have to setup some variables in this file as an initialisation:<br>
1. '''name''' is a string and contains the name associated with the channel by AnyWave.<br/>
+
Indeed, you may decide to compile your plugin to distribute it as a standalone software. In this case, use the following code in your main.m:<br/>
Note that if a reference has been set in AnyWave the name will contain the name of the reference channel (for example: A1 - A2 in a bipolar montage).<br/>
+
 
+
2. '''ref''' is a string and contains the reference channel set for the current channel. The string is empty if no reference is set.<br/>
+
 
+
3. '''samplingRate''' is a double value containing the data sampling rate in Hertz.<br/>
+
 
+
Note that the messages that usually go to the Matlab Command Window, like those coming from disp() or fprintf() will be available in AnyWave.<br/>
+
AnyWave will get the content of all text outputs from the Matlab script and put it in the process's log.<br/>
+
 
+
==AwGetData()==
+
As explained in AwGetInputChannels(), AnyWave will set some channels as input for the plug-in. AwGetData() will request data for these channels.
+
 
+
There are two mandatory parameters, the data position in file and the duration. Both parameters must be expressed in seconds.<br/>
+
Let's see an example:
+
 
<syntaxhighlight lang="matlab">
 
<syntaxhighlight lang="matlab">
function main()
+
function main(varargin)
%MAIN Summary of this function goes here
+
global args;
%  Detailed explanation goes here
+
if isdeployed
 
+
% STANDALONE AnyWave Plugin code
% ask for data, at position 0 and for a duration of 20 seconds.
+
  global host;
channels = AwGetData(0, 20);
+
  global port;
% plot only the first channel.
+
  global pid;
plot(channels(1).data);
+
% Wait for the user to close the figure.
+
uiwait();
+
  
 +
  if (nargin < 3)
 +
      error('missing arguments.');
 +
  end
 +
  host = varargin{1};
 +
  port = str2num(varargin{2});
 +
  pid = str2num(varargin{3});
 +
  if (nargin > 3)
 +
    args = varargin{4};
 +
  end
 +
 
 +
  assignin('base', 'host',  host);
 +
  assignin('base', 'port', port);
 +
  assignin('base', 'pid', pid);
 +
  assignin('base', 'args', args);
 +
% end of STANDALONE AnyWave Plugin code
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Like in the AwGetInputChannels() example above, channels will be an array of structures containing informations about input channels.<br/>
+
=Copy the plug-in to the right location=
The difference here is that the data are provided by AnyWave. The data vector for a channel is available in the field named '''data'''.
+
We are ready to add the plug-in to AnyWave.
 +
 
 +
Copy the folder MyPlugin to your user's AnyWave plugins directory. Remember to place it in the Matlab subfolder.
 +
For example on Windows:
 +
[[File:Matlab3.png|center]]
 +
 
 +
As you can see MyPlugin is located in the user's AnyWave path for Matlab Scripted plug-ins.
 +
 
 +
=Use the plug-in in AnyWave=
 +
Launch AnyWave: the plug-in should be shown as available.
 +
[[File:Matlab4.png|center]]

Latest revision as of 14:17, 30 March 2020

Introduction

This section targets people who have a good knowledge and practice of the MATLAB programming language.
The purpose is to explain how to write a MATLAB function that will be the heart of a plug-in executed in MATLAB by AnyWave.
We will also explain how to create a text file to describe our plug-in to AnyWave
The AnyWave-MATLAB API consists in a set of MATLAB functions. They will be described in details in a dedicated section.

Setup the plugin folder

The first thing to do is to create the basic structure for a plug-in.
A MATLAB Scripted plug-in is very simple: it is a folder containing at least two files.

Let's begin by creating a folder (called MyPlugin) somewhere on the computer.
This can be done in MATLAB: create a folder and create a new function called main in that folder.
The main function is MANDATORY. It will be the main function AnyWave will call to execute our plugin.

Matlab1.png

As shown in the image above, a MyPlugin folder has been created and a main function was added.

We are now ready to write our first Matlab plug-in!!

Refer to the AnyWave-MATLAB functions section to learn how to program a plug-in that will communicate with AnyWave.

Writing the desc.txt file

We have written the MATLAB code and now all we have to do is to create a descriptive text file to inform AnyWave about our plug-in.
Let's dot it in Maltab:

Matlab2.png

The file must be named desc.txt and may looks like:

name = My Matlab Plugin
description = I am a plug-in written in Matlab
category = Process:MATLAB:My MATLAB Plugin

The syntax is to set keywords and values.

Here we have three keywords (name, description, category).
Two keywords are mandatory : name and description. Other keywords are optional.

desc.txt keywords

Some keywords may have several values. Separate the values by the colon character :
The table below shows all the keywords handled by AnyWave and their functions.

keyword description type
name the name of your plugin. Must be unique. MANDATORY
description short description MANDATORY
category Where to link the plugin in AnyWave menus OPTIONAL
input_flags what is required for input OPTIONAL
flags The plugin special flags OPTIONAL

category

category: It tells AnyWave where the plug-in will appear in the menus. Here, we decided to make it appear under the Python sub-menu in the Processes main menu.
The category feature is usefull to separate plug-ins by theme.
Three category keywords are recognized:

  • Process : The plug-in will be set in the Processes menu with a subcategory and a name, for example 'Process:Correlation:Compute correlation'
  • File: The plug-in will be set in the File Menu under the Export sub-menu. Example : 'File:Export to file.'
  • View: The plug-in will be set in the View Menu. Example : 'View:Launch 3D viewer'

If no category is specified, AnyWave will set the plug-in in the Processes menu using the name defined in the file.

input_flags

input_flags: a list a ":" separated strings that set the input flags for the plugin.

input_flag description
GetAllMarkers AnyWave will copy all the markers and set them as input for the plugin. This will impact aw_getmarkers() function.
GetDurationMarkers AnyWave will copy markers with a duration and set them as input. This will impact aw_getmarkers() function
ProcessIgnoresChannelSelection The plugin will be run without using the selected channels as input by default.
ProcessRequiresChannelSelection The plugin will only run if the user has selected channels.
GetAsRecordedChannels AnyWave will copy all the as recorded channels found in the current file as input for the plugin. This will impact aw_getdata() function.
GetCurrentMontage AnyWave will copy the current montaged channels and set them as input for the plugin. This will impact aw_getdata() function.

Example of desct.txt with input_flags:

name = MyPlugin
description = do something in MATLAB
category = Process:Test:MyPlugin
input_flags = ProcessIgnoresChannelSelection:GetAllMarkers

flags

flags are used to configure the plugin capabilities.

flag description
NoDataRequired or ProcessDoesntRequireData indicates the plugin doesn't need a file to be open in AnyWave to run.
CanRunFromCommandLine indicates that the plugin can also run in batch mode and using the command line.

main.m

This is the entry point function called by AnyWave.
Depending on the type of plugin you are writing, you may have to setup some variables in this file as an initialisation:
Indeed, you may decide to compile your plugin to distribute it as a standalone software. In this case, use the following code in your main.m:

function main(varargin)
global args;
if isdeployed
% STANDALONE AnyWave Plugin code
   global host;
   global port;
   global pid;
 
   if (nargin < 3)
       error('missing arguments.');
  end
  host = varargin{1};
  port = str2num(varargin{2});
  pid = str2num(varargin{3});
  if (nargin > 3)
    args = varargin{4};
  end
 
  assignin('base', 'host',  host);
  assignin('base', 'port', port);
  assignin('base', 'pid', pid);
  assignin('base', 'args', args);
% end of STANDALONE AnyWave Plugin code
end

Copy the plug-in to the right location

We are ready to add the plug-in to AnyWave.

Copy the folder MyPlugin to your user's AnyWave plugins directory. Remember to place it in the Matlab subfolder. For example on Windows:

Matlab3.png

As you can see MyPlugin is located in the user's AnyWave path for Matlab Scripted plug-ins.

Use the plug-in in AnyWave

Launch AnyWave: the plug-in should be shown as available.

Matlab4.png