function setupSim(ModelName)
%% Function to setup simulations using a text file description
%% Look at examples in the subdirectory simulations
%% Copyright (C) by Charles. H. Anderson and Chris Eliasmith (All Rights Reserved)
%% Dept. Anatomy and Neurobiology
%% Washington Univ. School of Medicine
%% St. Louis, MO
%% cha@wustl.edu
%% eliasmith@uwaterloo.ca)
%% Modified Dec. 5, 2003 CHA
%% Feb. 25, 2001 Consolidation
%% Feb. 21, 2001
%% Major revision, Sept. 28, 2002
%% Added Dynamics Matrix as an option Oct. 29, 2003 - CHA
%% Runsimulation will automatically change the recurrent matricies to tau_syn*A + 1;
%% And all other coupling matricies to tau_syn;
%% The program is signaled through connection.form = 3 ( A hack -cha)
% clear all;
fclose('all');
DataBase = ['.', filesep];
FileName = [ModelName,'.txt'];
if(~exist(FileName))
errstr = sprintf('Could not open text file %s\n',FileName);
error(errstr);
end
fid = fopen(FileName,'r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Ignore comments and blank lines at the top of the file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = 1;
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Find the number of ensembles in the model
%% The first number in the first data line
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parse = ltext;
[word,parse] = strtok(parse);
NumEns = str2double(word);
while(isnan(NumEns)&length(word))
[word,parse] = strtok(parse);
NumEns = str2double(word);
end
if(isnan(NumEns))
errstr= sprintf('The first non-comment line must provide the number of ensembles files the simulation uses\n');
error(errstr);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Initialize Storage for Structures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
RLsoma = struct('gain',[],'bias',[],'input',[],'output',[]); %These must be in all soma structures
LIFsoma = struct('gain',[],'bias',[],'input',[],'output',[]...
,'volt',[],'trc',0.005,'tref',0.001,'inref',[]);
Ensemble = repmat(struct('Name','','N',1,'D',1,'Radius',1.0,...
'encvec',cell(1),'decvec',cell(1),'fcoefs',cell(1),'decfun',cell(1),...
'vecval',cell(1),'funval',cell(1),'Method',1,'ModelType',2,'soma',cell(1),'title',''),NumEns,1);
Synapse = struct('tc',0.005,'psc',[],'sysval',[]);
Connections = repmat(struct('extID',0,'inputMode',0,'form',0,'matrix',cell(1)...
,'weights',cell(1),'synapse',Synapse),NumEns,NumEns);
%% inputMode ( 0= no connection, 1 vector input, 2 function input)
%% form (0 undefined, 1 use system variables, 2 use coupling weights, 3 use dynamics matrix)
ltext = strjust(fgetl(fid),'left');
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read Ensemble Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for(l=1:NumEns)
parse = ltext;
% [word,parse] = strtok(parse);
% if(lower(word(1:3)) ~= 'ens')
% fprintf('Line %d: %s\n',lineNum, ltext);
% error('Expected the word "[Ens]emble".');
% end
[word,parse] = strtok(parse);
n = str2double(word);
while(isnan(n)&length(word))
[word,parse] = strtok(parse);
n = str2double(word);
end
if(isnan(n))
fprintf('Did not find a neuronal ensemble number/index on line %d\n',lineNum);
fprintf('Are you trying to define a function output here?\n');
fprintf('Keywords are [pol]ynomial, [con]stant,[lin]ear, [quad]ratic or [bil]inear\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
[word,parse] = strtok(parse);
fileName = [word,'.mat'];
if ~exist(fileName)
DataBaseStr = 'NeuronData';
mpath = path;
k = findstr(DataBaseStr,mpath);
kend = k+length(DataBaseStr)-1;
while((k>1)&(mpath(k)~=';'));
k=k-1;
end
DataBase2 = [mpath(k:kend) filesep];
fileName = [DataBase2,word,'.mat'];
if( ~exist(fileName))
fprintf('Cannot find the neuronal ensemble %s specified on line %d\n',fileName,lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
end
load(fileName);
[title,parse] = strtok(parse);
if (length(title)==0 || title(1) == '(' || title(1) == '%')
title = strtok(word, '_N');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Fill in structure data fields
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ensemble(l).Name = word; %% Ensemble Name
Ensemble(l).title = title;
Ensemble(l).N = N;
Ensemble(l).D = D;
Ensemble(l).Radius = Radius;
Ensemble(l).encvec{1} = EncVec;
Ensemble(l).decvec{1} = DecVec;
Ensemble(l).ModelType = ModelType;
Ensemble(l).vecval{1} = zeros(D,1);
Ensemble(l).fcoefs{1} = [];
if(ModelType == 1)
Ensemble(l).soma{1} = RLsoma;
Ensemble(l).soma{1}.gain = NeuronParms(:,2);
Ensemble(l).soma{1}.bias = NeuronParms(:,5);
Ensemble(l).soma{1}.input = zeros(N,1);
Ensemble(l).soma{1}.output = zeros(N,1);
elseif(ModelType == 2)
Ensemble(l).soma{1} = LIFsoma;
Ensemble(l).soma{1}.gain = NeuronParms(:,2);
Ensemble(l).soma{1}.bias = NeuronParms(:,5);
Ensemble(l).soma{1}.input = zeros(N,1);
Ensemble(l).soma{1}.output = zeros(N,1);
Ensemble(l).soma{1}.volt = zeros(N,1);
Ensemble(l).soma{1}.trc = trc;
Ensemble(l).soma{1}.tref = tref;
Ensemble(l).soma{1}.inref = -ones(N,1);
else
fprintf('Unknown neuron model type specified on line %d',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Now set up output functions, if any
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
parse = ltext;
[word,parse] = strtok(parse);
if(length(word)>2)
if(D==1)
clear Poly;
if(lower(word(1:3)) == 'pol')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read in the polynomial values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc==0)
fprintf('Expected polynomial coefficients on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
np = nc;
Polynomial(1,:) = V'; nf = 1;
while(nc~=0)
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
if((length(ltext)==0)|(ltext(1)=='%')|(lower(ltext(1))=='e'))
break;
end
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc ~= np)
fprintf('Multiple polynomials must have the same length\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
else
nf = nf+1;
Polynomial(nf,:) = V';
end
end
if(np>6)
fprintf('Error in the coefficient values on line %d\n',lineNum);
fprintf('Number of polynomial coefficients %d > max of 6\n',np);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Ensemble(l).decfun{1} = getPolyDec(Polynomial,Ensemble(l).encvec{1},Cmatrix,Moments,10,0);
Ensemble(l).funval{1} = zeros(nf,1); % Dimension of output function
Ensemble(l).fcoefs{1} = Polynomial;
if(nf==1)
fprintf(' outputs a polynomial with %d coefficients\n',np);
else
fprintf(' outputs %d polynomials with %d coefficients\n',nf,np);
end
end
else %% Functions for D>1
Const=[]; Linear=[]; Quad=[];
flag = 0;
while(~flag&(length(word)>2))
if(lower(word(1:3)) == 'con')
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
parse = ltext;
[values, parse] = strtok(parse,';');
V = sscanf(values,'%g');
[nc,junk] = size(V);
if(nc==0)
fprintf('Error in the constant values on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Const = V';
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
elseif(lower(word(1:3)) == 'lin')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read in the linear values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc==0)
fprintf('Expected linear coefficients on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(nc ~= D)
fprintf('Expected linear coefficients on line %d\n',lineNum);
fprintf('Number of linear coeficients %d must equal D=%d\n',nc,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Linear(:,1) = V; nf = 1;
while(nc~=0)
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
if((length(ltext)==0)|(ltext(1)=='%')|(lower(ltext(1))=='e'))
break;
end
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc == 0)
break;
end
if(nc ~= D)
fprintf('Number of linear coefficients must be the same on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
else
nf = nf+1;
Linear(:,nf) = V;
end
end
elseif((lower(word(1:3)) == 'qua')|(lower(word(1:3)) == 'bil'))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read in the quadratic values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
parse = ltext;
k1=1;k2=1;
while(length(parse))
[values, parse] = strtok(parse,';,:');
V = sscanf(values,'%g');
[nc,junk] = size(V);
if(nc==0)
fprintf('Error in the quadratic coefficient values on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Quad(k1,:,k2) = V'; k2 = k2+1;
end
while(nc~=0)
nc=0;
ltext = deblank(strjust(fgetl(fid),'left')); lineNum = lineNum+1;
if((length(ltext)==0)|(ltext(1)=='%')|(lower(ltext(1))=='e'))
break;
end
parse = ltext;
k1 = k1+1; k2 = 1;
while((length(parse))&(parse(1)~='%'))
[values, parse] = strtok(parse,';:');
parse = strjust(parse,'left');
values = strjust(values,'left');
if(values(1)=='%')
break;
end
V = sscanf(values,'%g');
[nc,junk] = size(V);
if(nc==0)
fprintf('Error in the quadratic coefficient values on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Quad(k1,:,k2) = V'; k2 = k2+1;
end
end
elseif(lower(word(1:3)) == 'pol')
fprintf('Polynomials not allowed for D>1 on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
else
flag=1; % exit if word(1:3 or 4) not one of above
end
%% Skip blank lines and comments
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
parse = ltext;
[word,parse] = strtok(parse);
end
if((~isempty(Const))|(~isempty(Linear))|(~isempty(Quad)))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Check Consistance: Const(1,nf),Linear(nf,D),Quad(D,D,nf)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(~isempty(Const))
[c1,nf] = size(Const);
if(c1~=1)
fprintf('Error in constant coefficients on line %d\n',lineNum);
fprintf('Number of rows in constant array %d, must be 1\n',c1);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(~isempty(Linear))
[l1,l2] = size(Linear);
if(l2~=nf)
fprintf('Number of constant values and linear terms are not equal on line %d\n',lineNum);
fprintf('const function number = %d, linear number = %d\n',nf,l2);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(l1~=D)
fprintf('Error in linear coefficients on line %d\n',lineNum);
fprintf('Dimension of linear terms %d are not equal to dimension D %d\n',l1,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(~isempty(Quad))
[q1,q2,q3] = size(Quad);
if(q3~=nf)
fprintf('Number of constant values and quad matricies are not equal on line %d\n',lineNum);
fprintf('const function number = %d, quad number = %d\n',nf,q3);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if((q1~=D)|(q2~=D))
fprintf('Error in quadratic matrix on line %d\n',lineNum);
fprintf('Dimensions of quadratic matrix %d,%d are not equal to space D %d\n',q1,q2,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
else
Quad = zeros(D,D,nf);
end
else
Linear = zeros(D,nf);
if(~isempty(Quad))
[q1,q2,q3] = size(Quad);
if((q1~=D)|(q2~=D))
fprintf('Error in quadratic matrix on line %d\n',lineNum);
fprintf('Dimensions of quadratic matrix %d,%d are not equal to space D %d\n',q1,q2,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(q3~=nf)
fprintf('Error in quadratic matrix on line %d\n',lineNum);
fprintf('const function number = %d, quad number = %d\n',nf,q3);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
else
Quad = zeros(D,D,nf);
end
end
elseif(~isempty(Linear))
[l1,nf] = size(Linear);
Const = zeros(1,nf);
if(~isempty(Quad))
[q1,q2,q3] = size(Quad);
if((q1~=D)|(q2~=D))
fprintf('Dimension of matrix is not consistent with Dimension of Vector Space on line %d\n',lineNum);
fprintf('Dimensions of quadratic matrix %d,%d are not equal to space D %d\n',q1,q2,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(q3~=nf)
fprintf('Error in quadratic matrix on line %d\n',lineNum);
fprintf('const function number = %d, quad number = %d\n',nf,q3);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
else
Quad = zeros(D,D,nf);
end
else
[q1,q2,nf] = size(Quad);
if((q1~=D)|(q2~=D))
fprintf('Error in quadratic matrix on line %d',lineNum);
fprintf('Dimensions of quadratic matrix %d,%d are not equal to space D %d\n',q1,q2,D);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Const = zeros(1,nf);
Linear = zeros(D,nf);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Compute the decoding vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Nsvd = (D+1)*(D+2)/2 + 5;
Ensemble(l).decfun{1} = getFuncDec(Const,Linear,Quad,Ensemble(l).encvec{1},Cmatrix,Moments,Nsvd,0);
Ensemble(l).funval{1} = zeros(nf,1); % Dimension of output function
Ensemble(l).fcoefs{1} = cell(3,1);
Ensemble(l).fcoefs{1}{1} = Const;
Ensemble(l).fcoefs{1}{2} = Linear;
Ensemble(l).fcoefs{1}{3} = Quad;
fprintf('Ensemble %d outputs a function of dimension %d\n',l, nf);
end
end
end %% End of test to see if there are function outputs
%% Skip blank lines and comments
while((length(ltext)==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
if((l~=NumEns)&(length(word)>2))
if(lower(word(1:3)) == 'inp')
fprintf('Expected another data file on line %d\n',lineNum);
fprintf('Check the number of data files\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Setup connections
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Ignore comments and blank lines
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
parse = ltext;
[word,parse] = strtok(parse);
if((length(word)<3)|(lower(word(1:2)) ~= 'in'))
fprintf('The inputs should start on or near line %d.\n',lineNum);
fprintf('Check the number of data files %d\n',NumEns);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
fprintf('*****Connections*****\n');
for(l=1:NumEns)
parse = ltext;
[word,parse] = strtok(parse);
if(lower(word(1)) =='e')
fprintf('Unexpected end of file %s',ModelName);
fprintf('Check the number of input connection descriptors\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(lower(word(1:2)) ~= 'in')
fprintf('Expected the word "[In]puts" on line %d.\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
fprintf('Inputs to %d:\n',l)
[word,parse] = strtok(parse);
m = str2double(word);
while(isnan(m)&length(word))
[word,parse] = strtok(parse);
m = str2double(word);
end
if(isnan(m))
fprintf('Could not find the input index on line %d.\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(m~=l)
fprintf('Inputs out of sequence on line %d.\n',lineNum);
fprintf('Expected inputs to %d, found %d\n',l,m);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
%% Skip blank lines and comments
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
t1 = 0; t2 = 0;
%% loop through the connections to this ensemble
while(~(t1|t2))
externalFlag = 0;
%% Skip blank lines and comments
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
end
[word,parse] = strtok(ltext);
if(lower(word(1:3)) == 'ext')
[word,parse] = strtok(parse);
m = str2double(word);
while(isnan(m)&length(word))
[word,parse] = strtok(parse);
m = str2double(word);
end
if(isnan(m))
fprintf('Could not find the external connection index on line %d.\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
fprintf(' gets input from external %d\n',m)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Connections(l,l).extID = m;
externalFlag = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif(lower(word(1:3)) == 'out')
[word,parse] = strtok(parse);
m = str2double(word);
while(isnan(m)&length(word))
[word,parse] = strtok(parse);
m = str2double(word);
end
if(isnan(n))
error('Did not find the ensemble output index/number on line %d.\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(m>NumEns)
fprintf('Input ensemble number %d > number of ensembles\n',m,NumEns);
fprintf('Line %d: %s\n',lineNum, ltext);
error('Connection to non existant ensemble');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(l~=m)
fprintf(' gets output from ensemble %d\n',m);
else
fprintf(' gets recurrent input from itself\n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(Connections(l,m).inputMode)
fprintf('The code does not support both vector and function inputs to same ensemble\n');
fprintf('Ensemble %d already gets the function output from %d\n',l,m);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Connections(l,m).inputMode = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif((lower(word(1:3)) == 'fun')|(lower(word(1:3)) == 'tra')) %Function or Transformation
[word,parse] = strtok(parse);
m = str2double(word);
while(isnan(m)&length(word))
[word,parse] = strtok(parse);
m = str2double(word);
end
if(isnan(m))
fprintf('Did not find ensemble number/index for a function input on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(length(Ensemble(m).funval)==0)
fprintf('Connection to nonexisting function output on line %d.\n',lineNum);
fprintf('Ensemble %d does not provide an output function\n',m);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(m>NumEns)
fprintf('Connection to nonexistant ensemble on line %d\n',lineNum);
fprintf('Input ensemble number %d > number of ensembles\n',m,NumEns);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(l~=m)
fprintf(' gets the function output from ensemble %d\n',m)
else
fprintf(' gets recurrent connection from its function output\n')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(Connections(l,m).inputMode)
fprintf('The code does not support both vector and function inputs to same ensemble\n');
fprintf('Ensemble %d already gets the vector output from %d\n',l,m);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
Connections(l,m).inputMode = 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
fprintf('Must have "[Ext]ernal", "[Out]put", or "[Fun]ction" on line %d\n.',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
parse = ltext;
[word,parse] = strtok(parse);
if(externalFlag==0) % Connected to another ensemble
if((lower(word(1:3))~='mat')&(lower(word(1:3))~='cou')&(lower(word(1:3))~='dyn'));
fprintf('Must have the word "[Cou]pling","[Mat]rix" or "[Dyn]amics" on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
if(lower(word(1:3))=='dyn')
Connections(l,m).form = 3;
fprintf(' %d connects to %d through a dynamics matrix.\n',l,m);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Now read in the matrix values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc==0)
error('Expected coupling matrix coefficients on line %d\n',lineNum);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
k=1; clear M;
M(k,:) = V'; k = k+1;
while(nc~=0)
ltext = strjust(fgetl(fid),'left'); lineNum = lineNum+1;
if((length(ltext)==0)|(ltext(1)=='%')|(lower(ltext(1))=='e'))
break;
end
V = sscanf(ltext,'%g');
[nc,junk] = size(V);
if(nc ~= 0)
M(k,:) = V'; k = k+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Make sure the dimensions of the Matrix are consistent
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[d1,d2] = size(M);
Dcol = Ensemble(l).D;
if(Connections(l,m).inputMode == 1);
Drow = Ensemble(m).D;
elseif(Connections(l,m).inputMode == 2)
[junk,Drow] = size(Ensemble(m).decfun{1});
else
if(Connections(l,m).extID == 0);
fprintf('Must have an external or internal connection on line %d\n',lineNum);
fprintf('l=%d,m=%d, Connections(l,m).inputMode=%d\n',l,m,Connections(l,m).inputMode);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
end
if((d1==1)&(d2==1))
if((Drow~=Dcol))
fprintf('Connection matrix size error on line %d\n',lineNum);
fprintf('Connection(l=%d,m=%d),size(M)=[%d,%d],D(l)=%d,D(m)=%d\n',l,m,d1,d2,Dcol,Drow);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
elseif((d1~=Dcol)|(d2~=Drow))
fprintf('Connection matrix size error on line %d\n',lineNum);
fprintf('Connection(l=%d,m=%d),size(M)=[%d,%d],D(l)=%d,D(m)=%d\n',l,m,d1,d2,Dcol,Drow);
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(Connections(l,m).form==0)
Connections(l,m).form = 1; % Default is to use system vectors (2 is weights)
end
Connections(l,m).synapse.sysval = zeros(Dcol,1);
Connections(l,m).matrix{1} = M;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else % We just set up an external connection and should not see the word 'Matrix'
if((length(word)>2)&(lower(word(1:3))=='mat'))
fprintf('External variables cannot have a coupling matrix set\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
end
while((length(deblank(ltext))==0)|(ltext(1)=='%'));
lineText = fgetl(fid);
if(lineText<0)
fprintf('Did not find the file termination word "End"!!!!\n');
error('Unexpected end of file %s',ModelName);
end
ltext = strjust(lineText,'left'); lineNum = lineNum+1;
end
parse = ltext;
[word,parse] = strtok(parse);
t1 = strcmp(lower(word(1:2)),'in');
t2 = strcmp(lower(word(1:3)),'end');
end
end
if(t1)
fprintf('Inconsistent number of data files and interconnections\n');
errstr = ['Error on Line ' num2str(lineNum) ': ' ltext];
error(errstr);
end
outfile = [DataBase,ModelName];
save(outfile,'NumEns','Ensemble','Connections');
fprintf('Saved %s\n',[outfile,'.mat']);
fclose all;