function [status]=exp_run(connectionMat, encoding, simItt, permute, numTrials, ci, name)
% INPUT: fsm_connect - connection matric; Ecost, k - max itteration
% OUTPUT: 

if ~exist('connectionMat','var') ||  ~exist('encoding','var'),
    print('Missing Needed Arguments!');    
end

if ~exist('simItt','var')||isempty(simItt),
   simItt = 500; 
end

if ~exist('permute','var')||isempty(permute),
    permute = 0;
end

if ~exist('numTrials', 'var')||isempty(numTrials),
    numTrials = 10;
end

if ~exist('ci', 'var')||isempty(ci),
    ci = .05;
end

if ~exist('name', 'var')||isempty(name),
  name = strcat('AutoGen_', datestr(now));   
end

resname = strcat(name,'_itt_', num2str(simItt), '_X_', num2str(numTrials), '_ci_' , num2str(100*ci), '.csv');
stimname = strcat(name, '_itt_', num2str(simItt), '_X_', num2str(numTrials), '_ci_' , num2str(100*ci), '.matstim');
statename = strcat(name, '_itt_', num2str(simItt), '_X_', num2str(numTrials), '_ci_' , num2str(100*ci), '.matstates');
transname = strcat(name, '_itt_', num2str(simItt), '_X_', num2str(numTrials), '_ci_' , num2str(100*ci), '.mattrans');

ConnectVector = reshape(connectionMat.',[],1)';

myFile = fopen(resname, 'w');
fprintf(myFile,'%s\n', 'i, mean(myTranCost) , var(myTranCost), myR_ST, myP_ST , myRLO_ST, myRUP_ST,  mean(myStateCost), var(myStateCost), myR_SS, myP_SS, myRLO_SS, myRUP_SS, myEncoding');
fclose(myFile);

myEncoding = encoding;
myResults=[];
myStimV=[];
myStatesV=[];
myTransV=[];
abrigedResults=[];

for i = 1:numTrials
    if permute,
       myEncoding = myEncoding(randperm(size(myEncoding,2))); 
    end
    [myTranCost, myStateCost, myStates, myTran, tranVec] = fsm_sim(connectionMat, myEncoding, simItt);
    myStimV = [myStimV; tranVec];
    myStatesV=[myStatesV; [1, myStates]];
    myTransV=[myTransV; myTran];			
    [myR_ST,myP_ST, myRLO_ST, myRUP_ST] = corrcoef(myTran',myTranCost','alpha', ci);
    [myR_SS,myP_SS, myRLO_SS, myRUP_SS] = corrcoef(myStates',myStateCost','alpha', ci);
    myResults = [myResults; [i, mean(myTranCost), var(myTranCost), myR_ST(1,2), myP_ST(1,2), myRLO_ST(1,2), myRUP_ST(1,2),  mean(myStateCost), var(myStateCost), myR_SS(1,2), myP_SS(1,2), myRLO_SS(1,2), myRUP_SS(1,2), myEncoding]];
    abrigedResults = [abrigedResults; [mean(myTranCost), var(myTranCost), myR_ST(1,2), myP_ST(1,2),mean(myStateCost), var(myStateCost), myR_SS(1,2), myP_SS(1,2)]];
end

dlmwrite(stimname, myStimV) ;
dlmwrite(statename, myStatesV) ;
dlmwrite(transname, myTransV) ;

dlmwrite(resname, myResults , '-append') ;
status = abrigedResults;
%results = csvread(name, 1,0);
%dlmwrite(name, mean(results) , '-append') ;


