function theanswer = pfor(itervar, from, to, returnthis, filedep, jobdata, body)


%PFOR, the parralellized for loop
%The syntax is:   theanswer = pfor(itervar, from, to, returnthis, newdir, setenv, body)
%Where itervar is a string containing the variable name you wish to use for the iterator. 
%Itervar's variable can be referenced in the body of your loop statement.
%From and to are the limits of the iteration. The function will step in increments of one from 'from' to 'to'.
%Returnthis is a cell array of strings containing variable names to be returned
%FileDep is a cell array of strings describing files and directories to add to the path.
%Jobdata is a single data structure to be made visible to all workers
%Body is a single string containing ';' separated statements that compromise the body of each 'for' iteration.
%Please note that it is not possible to "accumulate" a variable value using pfor due to the distributed nature of the underlying code. 
%It is possible to populate an array through the for loop. The mechanism can handle cell arrays but not embedded cell arrays.
%Body can accept a script if the script filename is specified (with extension) as seen relative to the calling instance of matlab.

nruns = to-from;

currentvars = evalin('base', 'who');
s = size(currentvars);
s = s(1);
for  i = 1:s
	evalin('base', strcat('assignin(''caller'',''',currentvars{i},''',',currentvars{i},')'));
end
currentvars = evalin('caller', 'who');
s = size(currentvars);
s = s(1);
for  i = 1:s
	evalin('caller', strcat('assignin(''caller'',''',currentvars{i},''',',currentvars{i},')'));
end

therundata = {};
backupbody=body;
mystring = strcat('parrans={', returnthis{1});
temp = size(returnthis);
temp = temp(2);
if (temp > 1)
	for myiterator = 2:temp
		mystring = strcat(mystring, ',' , returnthis{myiterator});
	end
end
returnstring = strcat(mystring, '};');
fxnstring = strcat('function parrans=pforfxn(',itervar, '),');
try
	myfid = fopen(backupbody);
	tline = fgetl(myfid);
	body = '';
	while(~(tline == -1))             
		body = strcat(body, tline);
		tline = fgetl(myfid);
	end
	fclose('all');
catch
	parrans = lasterr;
end
mystring = strcat(fxnstring , body , ';' , returnstring);

fid = fopen('pforfxn.m', 'wt');
fprintf(fid, '%s', mystring);
fclose(fid);

save('for.mat');

s=size(filedep);
s=s(2);
filedep{s+1}='for.mat';
filedep{s+2}='pforfxn.m';

jm=findResource('jobmanager','LookUpURL','rescluster2.mgh.harvard.edu');
j=createJob(jm,'fileDependencies',filedep);

set(j, 'JobData', jobdata)

for iterator = from:to
	createTask(j,'pforfxn',1,{iterator});
end

submit(j);

while (~strcmp(get(j,'state'),'finished'))
%disp(get(j));
	pause(2);
end
theanswer=getAllOutputArguments(j);
%theanswer=j;
destroy(j);

temp = size(theanswer{to});
temp = temp(2);
returnelement = theanswer{to};
try
	if (strcmp(returnelement(1:5), 'ERROR'))
		disp('An error has occured outside loop');
		disp(returnelement);
		return;
	end
catch
end
for j = from:(to-1)
	inprogress = theanswer{j};
	try
		if (strcmp(inprogress(1:5), 'ERROR'))
			disp('An error has occured inside loop');
			disp(inprogress);
			return;
		end
	catch
	end
	for i = 1:temp
		thirdtemp = returnelement{i};
		fourthtemp = inprogress{i};
		tempsize = size(returnelement{i});
		tempsize1 = tempsize(1);
		tempsize2 = tempsize(2);
		clear tempsize3;
		try
			tempsize3 = tempsize(3);
		catch
		end
		if(~and((tempsize1 == 1), (tempsize2 == 2)))
			try
				tempsize3 = tempsize(3);
				thirdtemp(j,:,:) = fourthtemp(j,:,:);
			catch
				try
					thirdtemp{j} = fourthtemp{j};
				catch
					try
						thirdtemp(j) = fourthtemp(j);	
					catch
						thirdtemp = fourthtemp;	
					end
				end
			end
			returnelement{i} = thirdtemp;
		end
	end
end
theanswer = returnelement;
temp = size(returnthis);
temp = temp(2);
for i = 1:temp
	assignin('base',returnthis{i}, returnelement{i});
	assignin('caller',returnthis{i}, returnelement{i});
end
delete('for.mat');
delete('pforfxn.m');
warning on all;
return;