function [sanitised_cell_array] = fc_sanitise(original_cell_array, brainArea)
%======= FUNCTION SANITISE: Sanitise a cell array
% Author: James Allen
% Date: July 2006
% A function to search through a cell array of strings, removing those
% containing certain characters, changing other characters, and
% returning the resulting cell array. Purpose is to accept a list of
% www.CoCoMac.org sourceSite fields or targetSite fields, and remove
% ambiguous entries (those containing \ or /, e.g. V1/V2), those containing
% wildcards (#), or to change 'dangerous' characters
% brainArea also supplied for log entry purposes
[noOfUnsanitisedSites, dummy] = size(original_cell_array);
if noOfUnsanitisedSites == 0
error('Error - no source sites found in sourceSites file!')
end
sanitised_cell_array = {}; % Var for making a sanitised sourceSite list
log_sanitiseSources = {}; % For keeping a log of any changes made to original_cell_array
for sanitiseCounter = 1:noOfUnsanitisedSites
% Check for / \ or # - if present, the sourcesite will be discarded.
% Also discard if empty.
if isempty(strfind(original_cell_array{sanitiseCounter}, '/')) == 1
if isempty(strfind(original_cell_array{sanitiseCounter}, '\')) == 1
if isempty(strfind(original_cell_array{sanitiseCounter}, '#')) == 1
if isempty(original_cell_array{sanitiseCounter}) == 0 % If NOT empty
% If we got to here, the sourcesite must not contain a
% / \ or # and is not empty, so...
% Assign the source site to the sanitised source sites
% array:
sanitised_cell_array{end+1, 1} = original_cell_array{sanitiseCounter}; % If there were no / \ or #, then the string (with any characters changed) gets added to the new cell array
end % End of condition: if not empty
else
log_sanitiseSources{end+1, 1} = ['Dropped brain area ' original_cell_array{sanitiseCounter} ' because it contained a #'];
end % End of condition: if there is no '#' in the sourcesite name
else
log_sanitiseSources{end+1, 1} = ['Dropped brain area ' original_cell_array{sanitiseCounter} ' because it contained a \'];
end % End of condition: if there is no '\' in the sourcesite name
else
log_sanitiseSources{end+1, 1} = ['Dropped brain area ' original_cell_array{sanitiseCounter} ' because it contained a /'];
end % End of condition: if there is no '/' in the sourcesite name
end % End of sanitisation loop: going through all entries in original_cell_array, removing those with / \ or #, changing forbidden filename characters
%--------------------------------------
%==========================================================
%---- Save any changes to original_cell_array as a log file
%==========================================================
log_filename = 'sanitiseLog.txt';
try
log_file = fopen(log_filename, 'at'); % a = append t = text
catch
error(['Error opening sanitiseLog file :' lasterr])
end
[noOfChanges dummy] = size(log_sanitiseSources);
logMatrix = char(log_sanitiseSources); % convert to char array so it can be written to file
%--------------------------------------
%---- Save procedure if we are working on startNodes
if nargin == 1 %If only 1 argument supplied, it must be a call to go through a list of sourceSites
fprintf(log_file, '%s \n', datestr(now)); %Add a timestamp to the log
if noOfChanges == 0
fprintf(log_file, '%s \n', 'No changes made to sourceSite names');
else
fprintf(log_file, '%s \n', 'Changes made to sourceSites: ');
for saveCounter = 1:noOfChanges
fprintf(log_file, '%s \n', logMatrix(saveCounter, :));
end
end
%--------------------------------------
%---- Save procedure if we are working on endNodes
elseif nargin == 2 %If 2 supplied, must have supplied an associated brain area (to put in log), so we're dealing with an endNode
if noOfChanges > 0
fprintf(log_file, '%s \n', ['Removed the following targetNode from sourceSite ' brainArea ':']);
for saveCounter = 1:noOfChanges
fprintf(log_file, '%s \n', logMatrix(saveCounter, :));
end
end
else
error('Error in sanitising function, problem ascertaining nargin')
end
fclose(log_file);
%==========================================================
% End of save changes
%==========================================================
return