You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: calculatePvalues.m
    path of file: ./repos/bspm/thirdparty/bramila/external/ISC_toolbox/ISCanalysis
    
    the code of the file until where you have to start completion: function pvals = calculatePvalues(vals,nullVals)

% This function returns p-values according to null distribution (generated
% by permutation testing). Returned p-values can then be used to perform,
% e.g. FDR correction. p-values are obtained via look-up tables from empirical distribution function. Several
% look-up tables are used to provide specific accuracy for different p-value 
% ranges.
%
% inputs: 
% vals - vector of true distribution values
% nullVals - vector of null distribution values

    
function pvals = calculatePvalues(vals,nullVals)

% This function returns p-values according to null distribution (generated
% by permutation testing). Returned p-values can then be used to perform,
% e.g. FDR correction. p-values are obtained via look-up tables from empirical distribution function. Several
% look-up tables are used to provide specific accuracy for different p-value 
% ranges.
%
% inputs: 
% vals - vector of true distribution values
% nullVals - vector of null distribution values
%
% outputs:
% pvals - p-values of the observations from true distribution. Values are 
% set to nan if they do not exceed the false discovery rate q. 

% Jukka-Pekka Kauppi 1.9.2009
% Tampere University of Technology


q = 0.05; % false discovery rate level
plotFigs = 1; % plot figures (on/off)

N = length(nullVals);
M = length(vals);

% Set look-up table intervals (from 0 to q):
intVals = [0 0.005 0.01 0.02 0.03 0.04 q];

% set approximate number of samples in look-up tables (accuracy):
acc = [floor(N*0.005) 200 750 100 100 100];




% get corresponding decimation factors:
acc = floor(N*diff(intVals)./acc);
acc(acc<1)=1;

% remove nan-values from null distribution and sort values:
nullVals = nullVals(:)';
nullVals(isnan(nullVals)) = [];
nullVals = sort(nullVals);
nullVals = fliplr(nullVals);
% find critical value according to:
% Nichols and Holmes: "Nonparametric Permutation Tests For 
% Functional Neuroimaging: A Primer with Examples", HBM 15, 2001.
critVal = nullVals(1+floor(N*q));
% remove nan-values and other non-interesting values from true distribution:
valsTmp = vals;
vals = vals(:)';
crapVals = isnan(vals) | vals < critVal;
vals(crapVals) = [];

% sort values into descend