Skip to content

Instantly share code, notes, and snippets.

@robertoostenveld
Last active January 30, 2018 16:17
Show Gist options
  • Save robertoostenveld/bf05b9ba0f7e33f14df9f6ae753de66c to your computer and use it in GitHub Desktop.
Save robertoostenveld/bf05b9ba0f7e33f14df9f6ae753de66c to your computer and use it in GitHub Desktop.
This demonstrates the plugin format for the FieldTrip fileio module.
function [varargout] = example_format(varargin)
% EXAMPLE_FORMAT demonstrates the plugin format for the FieldTrip fileio module.
%
% Use as
% hdr = ft_read_data('test.mff', 'headerformat', 'example_format')
% dat = ft_read_data('test.mff', 'headerformat', 'example_format', 'dataformat', 'example_format')
st = dbstack;
if length(st)>1 && strcmp(st(2).name, 'ft_read_header')
command = 'header';
elseif length(st)>1 && strcmp(st(2).name, 'ft_read_data')
command = 'data';
elseif length(st)>1 && strcmp(st(2).name, 'ft_read_event')
command = 'event';
elseif length(st)>1 && strcmp(st(2).name, 'ft_write_data')
command = 'write';
else
command = 'unknown';
end
nchan = 10;
fsample = 1000;
ntime = 60*fsample;
switch command
case 'header'
% filename = varargin{1};
% option1 = ft_getopt(varargin(2:end), 'option1')
% option2 = ft_getopt(varargin(2:end), 'option2')
% etc.
hdr = [];
hdr.Fs = fsample;
hdr.nChans = nchan;
hdr.nTrials = 1;
hdr.nSamples = ntime;
hdr.nSamplesPre = 0;
for i=1:nchan
hdr.label{i} = num2str(i);
hdr.chantype{i} = 'unknown';
hdr.chanunit{i} = 'unknown';
end
% return the header
varargout = {hdr};
case 'data'
% filename = varargin{1};
% begsample = ft_getopt(varargin(2:end), 'begsample')
% endsample = ft_getopt(varargin(2:end), 'endsample')
% etc.
dat = randn(nchan, ntime);
% return the data
varargout = {dat};
case 'event'
% filename = varargin{1};
% etc.
event = [];
event.type = 'start';
event.value = [];
event.sample = 1;
event.offset = 0;
event.duration = [];
% return the event structure
varargout = {event};
case 'write'
% write the data to file
error('not yet implemented');
otherwise
error('it is not clear what you expect from me');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment