Function definitions are not permitted in this context - Decoding_Toolbox

1 view (last 30 days)
Dear all,
I am trying to run Decoding toolbox MEG/EEG tutorial code (matlab 2014b)http://www.readout.info/tutorials/megeeg-decoding-tutorial/
But, as I run create_binned_data_from_raster_data
raster_file_directory_name = 'C:\...\Isik_26_letter_MEG_raster_data\'
save_prefix_name = 'Binned__data';
bin_width = 150;
step_size = 50;
create_binned_data_from_raster_data(raster_file_directory_name, save_prefix_name, bin_width, step_size);
i face an error: Function definitions are not permitted in this context
for the_bin_start_times in create_binned_data_from_raster_data
What does this mean and what is the solution?
% fix the directory name in case there it does not end with a slash
last_char = raster_file_directory_name(end);
if (strcmp(last_char, '/') + strcmp(last_char, '\') + strcmp(last_char, '*')) == 0
raster_file_directory_name = [raster_file_directory_name '/']
end
% if the directory ends with a name a * then use only those file names that have the string up to the *
% for example my_raster_directory/*PFC* will use only the files that have PFC in the file name
if strcmp(last_char, '*')
raster_file_dir = dir(raster_file_directory_name);
[t r] = strtok(raster_file_directory_name, '/');
while ~isempty(r)
[t r] = strtok(r, '/');
end
raster_file_directory_name = raster_file_directory_name(1:(end - length(t))); % remove the end string from the directory name
else
raster_file_dir = dir([raster_file_directory_name '*.mat']);
end
if isempty(raster_file_dir)
if isempty(raster_file_dir)
error('The directory name given does not exist');
else
error('The directory name given does not contain any .mat files');
end
end
if nargin < 5
start_time = 1;
end
if nargin < 6
load([raster_file_directory_name raster_file_dir(1).name]);
end_time = size(raster_data, 2);
end
if (length(bin_width) == 1) && (length(sampling_interval) == 1) % if a single bin width and step size have been specified, then create binned data that averaged data over bin_width sized bins, sampled at sampling_interval intervals
the_bin_start_times = start_time:sampling_interval:(end_time - bin_width + 1);
the_bin_widths = bin_width .* ones(size(the_bin_start_times));
elseif (length(bin_width) == 1) && (length(sampling_interval) > 1) % if multiple step size have been specified indicating bin start times, but only a single bin width is given, then create binned data that averaged data over bin_width sized bins, sampled using the start bin times given by sampling_interval
the_bin_start_times = c;
the_bin_widths = bin_width .* ones(size(the_bin_start_times));
elseif (length(bin_width) > 1) && (length(sampling_interval) > 1)
if length(bin_width) ~= length(sampling_interval), error('If a series of bin widths are specified by having bin_width be a vector, and a series of start binning times are specified by having sampling_interval be a vector, then the length(bin_width) must equal length(sampling_interval)'); end
the_bin_start_times = sampling_interval;
the_bin_widths = bin_width;
end
if (length(sampling_interval) > 1) && (nargin > 4)
error('If a series of start bin times are given by having sampling_interval be a vector, then one can not specify start_time and end_time arguments (i.e., the number of arguments to this function must be less than 5');
end
fprintf('\n');
% go through all the files and bin them
for i = 1:length(raster_file_dir)
% print a message the the data is being binned (and add a dot for each file that has been binned
curr_bin_string = [' Binning the data: ' num2str(i) ' of ' num2str(length(raster_file_dir))];
if i == 1
disp(curr_bin_string);
else
fprintf([repmat(8,1,bin_str_len) curr_bin_string]);
end
bin_str_len = length(curr_bin_string);
load([raster_file_directory_name raster_file_dir(i).name]);
curr_binned_data = bin_one_site(raster_data, the_bin_start_times, the_bin_widths); % use the below helper function to bin the data
binned_data{i} = curr_binned_data;
% save all the labels
the_label_names = fieldnames(raster_labels);
for iLabel = 1:length(the_label_names)
eval(['binned_labels.' the_label_names{iLabel} '{i} = raster_labels.' the_label_names{iLabel} ';']);
end
% save any extra neuron info
if ~isempty(raster_site_info)
the_info_field_names = fieldnames(raster_site_info);
for iInfo = 1:length(the_info_field_names)
if isstr( eval(['raster_site_info.' the_info_field_names{iInfo}]))
eval(['binned_site_info.' the_info_field_names{iInfo} '{i} = raster_site_info.' the_info_field_names{iInfo} ';']);
elseif isempty( eval(['raster_site_info.' the_info_field_names{iInfo}]))
% just ignore this field if it is empty...
else
eval(['binned_site_info.' the_info_field_names{iInfo} '(i, :) = raster_site_info.' the_info_field_names{iInfo} ';']); % might run into problems with this so above line could be more useful
end
end
else
binned_site_info = [];
end
end
% save extra information about the bin_width, sampling_interval, etc.
binned_site_info.binning_parameters.raster_file_directory_name = raster_file_directory_name;
binned_site_info.binning_parameters.bin_width = bin_width;
binned_site_info.binning_parameters.sampling_interval = sampling_interval;
binned_site_info.binning_parameters.start_time = start_time;
binned_site_info.binning_parameters.end_time = end_time;
binned_site_info.binning_parameters.the_bin_start_times = the_bin_start_times;
binned_site_info.binning_parameters.the_bin_widths = the_bin_widths;
% if there is a field in the in the raster_site_info that has the time that was used to align the data
% and if this time is the same for all raster files (as it should be) then save this time with the binning parameters
% this time can then be used by plotting objects to align the data to specify what time is 0.
if isfield(binned_site_info, 'alignment_event_time')
if sum(abs(diff(binned_site_info.alignment_event_time))) == 0
binned_site_info.binning_parameters.alignment_event_time = raster_site_info.alignment_event_time(1);
end
end
if (nargin < 5) && (length(bin_width) == 1) && (length(sampling_interval) == 1)
saved_binned_data_file_name = [save_prefix_name '_' num2str(bin_width) 'ms_bins_' num2str(sampling_interval) 'ms_sampled'];
elseif (nargin > 4) && (length(bin_width) == 1) && (length(sampling_interval) == 1)
saved_binned_data_file_name = [save_prefix_name '_' num2str(bin_width) 'ms_bins_' num2str(sampling_interval) 'ms_sampled_' num2str(start_time) 'start_time_' num2str(end_time) 'end_time'];
elseif (length(bin_width) == 1) && (length(sampling_interval) > 1)
saved_binned_data_file_name = [save_prefix_name '_' num2str(bin_width) 'ms_bins_custom_sampling'];
elseif (length(bin_width) > 1) && (length(sampling_interval) > 1)
saved_binned_data_file_name = [save_prefix_name '_custom_bins_and_custom_sampling'];
end
fprintf('\n')
disp([' Saving the binned data to the file: ' saved_binned_data_file_name])
%save(saved_binned_data_file_name, '-v7.3', 'binned_data', 'binned_labels', 'binned_site_info');
save(saved_binned_data_file_name, 'binned_data', 'binned_labels', 'binned_site_info');
function binned_data = bin_one_site(raster_data, the_bin_start_times; the_bin_widths)
% a helper function that bins the data for one site
for c = 1:length(the_bin_start_times)
binned_data(:, c) = mean(raster_data(:, the_bin_start_times(c):(the_bin_start_times(c) + the_bin_widths(c) -1)), 2);
end

Answers (1)

Steven Lord
Steven Lord on 14 Nov 2019
The ability to define local functions in scripts was introduced in release R2016b. Are you using an older release? If so you'll need to put that function in its own function file or make the whole file a function file by starting it with a function definition.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!