% getInputFile script

fclose('all'); % just in case data file is already open

% Get Java's input file:
oldinfile = input('Enter the Java input filename: ','s');
while length(oldinfile)==0 | ~exist(oldinfile)
disp(['The file called ',oldinfile,' does not exist!']);
oldinfile = input('Enter the Java input filename: ','s');
end

% Get input file for MATLAB (Java's output file):
newinfile = input('Enter the Java output file: ','s');
while length(oldinfile)==0 | ~exist(newinfile)
disp(['The file called ',newinfile,' does not exist!']);
newinfile = input('Enter the Java output file: ','s');
end
newdata = fopen(newinfile,'r');

% Get output file:
outfile = fopen(input('Enter a file to store results from MATLAB: ','s'),'w');

% Get problem data from the old input file:
oldinfiledata = dlmread(oldinfile,'\n');
firstrun = oldinfiledata(1);
lastrun = oldinfiledata(2);
numruns = oldinfiledata(3);

% could have also done something like $dlmread('data.in','\n',0,0)$ to read
% in all the data without opening the file.

% I could have saved myself a lot of work had I not printed
% non-numeric data in the Java output file. There are a bunch
% of ways, each more gruesome than the next. I could have also written the
% non-numeric data with %s! (see $help load$). Well, here's one idea:

% "Throw out" the first 2 lines from the Java file:
fgetl(newdata);
fgetl(newdata);

% Create a temporary data file to hold the "good" (numeric) data:
tmpfile = fopen('tmp','w');

% Strip out the remaining non-numeric data and store the numbers in temp file:
for count=firstrun:lastrun
fgets(newdata); % skip first line of each data set
for run=1:numruns
fprintf(tmpfile,'%s',fgets(newdata)); % dump values in tmp file
end
end

% Now, read in all that wonderful data from the temp file:
load 'tmp';
results = tmp;

% Clean up
fclose('all');