% function to scale data to AD converter [0,2^b-1]

function bits = units2AD(signal,file,b)

    signals = size(signal,1);
    samples = size(signal,2);
    % version 2008 Matlab
    %bits = round(mapminmax(signal,0,1023));
    % versiones antiguas
    bits = round(tramnmx(signal,min(signal,[],2),max(signal,[],2)));
    bits = round( postmnmx(signal,0*ones(1,signals)',2^b-1*ones(1,signals)'));    
    fid=fopen(file,'wt');
    fprintf(fid,'%d\n',reshape(bits,signals*samples,1));
    fclose(fid);