% extractpar - Extraction of data in .par and .rep file to Matlab/Octave environment % % Input: filename, a string variable in the current workspace % with the path to the .par and .rep files without the % extensions, e.g. use filename = '../../cod' to indicate % ../../cod.par and ../../cod.rep. % % Output: N, matrix with population trajectory, read from .rep file. % % The variables in the .par file, with the same names as in the % .par file. % % NOTE: The .par file must be EXACTLY on the form provided by ADMB. % The .rep file must contain only numbers (defining N) first. % % Written by Lennart Frimannslund, April 2010. % if (exist('filename','var') == 0), fprintf(1,'%s: No filename specified!\n',mfilename); return; end; % Get the .par file fid = fopen([filename '.par'],'r'); % Discard first line, which contains information about the objective function, etc. l = fgetl(fid); l = 0; % Process remaining lines, in pairs. The first line of the pair has the name, the second the value. while (l ~= -1), l = fgetl(fid); if (l == -1), break; end; varname = strip_hash_and_colon(l); l = fgetl(fid); % Send the variable name and values to the command line eval(sprintf('%s = [ %s ];',varname,l)); %sprintf('%s = [ %s ];',varname,l) end; fclose(fid); % Get the .rep file % Number of lines in the rep file n_lines = length(N0); N_cell = cell(n_lines,1); max_width_so_far = 0; fid = fopen([filename '.rep']); % Get one line at a time for (i=1:n_lines), output = fgetl(fid); N_cell{i} = str2num(output); max_width_so_far = max(max_width_so_far,length(N_cell{i})); end; fclose(fid); N = zeros(n_lines,max_width_so_far); for (i=1:n_lines), N(i,1:length(N_cell{i})) = N_cell{i}(:)'; end; % ...and clean up our temporary variables clear N_cell output max_width_so_far n_lines i return;