The anywave() function¶
A MATLAB plugin talks to AnyWave through one function: anywave().
It is provided by the AnyWave MATLAB interface (a mex file installed with AnyWave) and works the same way in a plugin
launched by AnyWave and in a debug session opened from the MATLAB prompt.
result = anywave('<command>', cfg)
- The first argument is always the command name, a string.
- The optional second argument is a struct (
cfg) that sets the options of the command. - Fields that AnyWave does not know are ignored, field names are case sensitive.
Commands¶
| command | what it does | returns | documentation |
|---|---|---|---|
init |
connects to AnyWave, must be called first | a struct with all the settings of the plugin | init |
get_data |
requests data (channels, time selections, filters...) | a struct array | get_data |
get_markers |
requests the markers of the current file | a struct array | get_markers |
send_markers |
adds markers to the current file | nothing | send_markers |
send_message |
writes a text in the AnyWave log (alias: send_text) |
nothing | send_message |
get_props |
reads the settings of the plugin again | a struct | get_props |
change_sig_prop |
changes the color or the filters of channels | nothing | change_sig_prop |
ica |
rejects (or restores) independent components | nothing | ica |
run |
runs another plugin on a file | nothing | run |
debug_connect |
connects to an AnyWave started in debug mode | a struct (same as init) |
debug |
Any other command name raises the error Unknown command.
A complete plugin¶
The plugin below reads the EEG channels, computes the RMS of each channel on windows of one second, marks the windows where the RMS is high and reports what it did in the AnyWave log.
function main(varargin)
% 1. connect to AnyWave and get the settings
args = anywave('init', varargin);
anywave('send_message', ['Processing ' args.data_file]);
% 2. get the EEG data, without any filter
cfg = [];
cfg.types = {'EEG'};
cfg.raw_data = true;
out = anywave('get_data', cfg);
if isempty(out)
error('This file has no EEG channel.');
end
data = double(out.data); % one row per channel, one column per sample
sr = out.channels(1).sr; % sampling rate in Hz
names = {out.channels.name};
% 3. RMS of every channel in windows of 1 s
win = round(sr);
nwin = floor(size(data, 2) / win);
threshold = 30; % in the unit of the channels (uV for EEG)
count = 0;
markers = struct('label', {}, 'position', {}, 'duration', {}, 'value', {}, 'color', {}, 'channels', {});
for w = 1:nwin
segment = data(:, (w - 1) * win + (1:win));
rms = sqrt(mean(segment .^ 2, 2));
for c = find(rms > threshold)'
count = count + 1;
markers(count).label = 'high_rms';
markers(count).position = (w - 1); % seconds
markers(count).duration = 1; % seconds
markers(count).value = rms(c);
markers(count).color = '#FF8800';
markers(count).channels = names(c); % cell array of labels
end
end
% 4. send the markers back and report
if count > 0
anywave('send_markers', markers);
end
anywave('send_message', sprintf('%d marker(s) sent', count));
end
Good to know¶
Data and units. get_data returns single precision values, one row per channel. EEG and sEEG are in µV, MEG in pT:
the unit field of each channel tells the unit.
Lists with one element. MATLAB converts the cfg struct to JSON before sending it, and jsonencode turns a vector
of one number into a plain number. Use a cell array when a field is a list that may contain a single value:
cfg.values = {10}; % list with one value (cfg.values = 10 is read as "no filter")
cfg.labels = {'Fp1'}; % list with one label
Single quotes. The command name and the text arguments must be char arrays (single quotes). A double quoted
"get_data" is a MATLAB string and raises anywave: First input must be a string.
Errors. A command that AnyWave cannot execute raises a regular MATLAB error. Errors detected by AnyWave start with ERROR:.
Use try / catch when you want your plugin to continue:
try
out = anywave('get_data', cfg);
catch err
anywave('send_message', ['get_data failed: ' err.message]);
end
Text with accents. Labels, messages and file paths may contain accents and any Unicode character with a recent
build of AnyWave. An older build stops with Can't create a std::string from this data.
Several requests. Every call to anywave() is an independent request. AnyWave answers them one at a time.
AnyWave stopped. The error Error while sending data to AnyWave. means that MATLAB cannot reach AnyWave: it was closed
or stopped. In a debug session, start AnyWave again and call debug_connect again.