function [initialparams,changedparams] = RandomiseNaN(params,datamean,dataSD)
% params is a vector containing [n p q qsigmapercentage QVtype noise pstim offset]
% This is a concise way of passing model data around.
% Free parameters are signalled by being set to NaN.
% This function chooses a suitable set of starting conditions by replacing
% all the NaNs in the vector initialparams with suitable guesses.
% Changedparams is a vector containing, in order, the entries of initialparams which were reset on this function call

initialparams=params;
if isnan(initialparams(1))
   initialparams(1) = fix(rand(5)*5)+1; %random value for n
end
if isnan(params(2))
   initialparams(2) = rand(1); %random value for p
end
if isnan(params(3))
   initialparams(3) = rand(1)*datamean; %random value for q
end
if isnan(params(4))
   initialparams(4) = rand(1)*100; %random value for qsigmapercent
end
if isnan(params(5))
   initialparams(5) = 1 + 0.5 * (rand(1)>0.5); %random value for QVtype
end
if isnan(params(6))
   initialparams(6) = rand(1)*dataSD; %random value for noise
end
if isnan(params(7))
   initialparams(7) = rand(1); %random value for pstim
end
if isnan(params(8))
   initialparams(8) = -datamean + rand(1) * datamean; %random value for offset
end
changedparams = initialparams(initialparams~=params);