function [output1, output2] = readSweep(type, channel_handle, numChannels, idx, channelInfo)
%Funtion to read sweeps of scaled or reduced values
%type               1: scaled sweep
%                   2: reduded sweep
%numChannels        number of channels
%idx                channel index

   if type == 1
       numDataSweeps = uint64(0);
       infoSweep(numChannels) = struct;
       [DMDReader_ErrorCode, channel_handle, numDataSweeps] = calllib('dmdlib', 'DMDReader_GetNumDataSweeps', channel_handle, numDataSweeps);
       if (DMDReader_ErrorCode ~= 0)
           close(f)
           error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetNumDataSweeps'])
       end

      % read sweeps
        for k = 0:numDataSweeps - 1
            numSweeps = uint64(0);
            sweep = libstruct('DMDReader_SWEEP');
            p_sweep = libpointer('DMDReader_SWEEP', sweep);
            [DMDReader_ErrorCode, channel_handle, sweep, ~] = calllib('dmdlib', 'DMDReader_GetDataSweeps', channel_handle, k, 1, p_sweep.Value, numSweeps);
            infoSweep(idx+1).channels(k+1).sweep = sweep;
        end %for k numDataSweeps
        if (DMDReader_ErrorCode ~= 0)
           close(f)
           error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetDataSweeps'])
        end
        output1 = infoSweep;
        output2 = numDataSweeps;

   elseif type == 2
        % number of reduced sweeps per channel
        numReducedSweeps = uint64(0);
        infoReducedSweep(numChannels) = struct;
        [DMDReader_ErrorCode, channel_handle, numReducedSweeps] = calllib('dmdlib', 'DMDReader_GetNumReducedSweeps', channel_handle, numReducedSweeps);
        if (DMDReader_ErrorCode ~= 0)
           close(f)
           error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetNumReducedSweeps'])
        end
        [~,sys] = memory;
        if (sys.PhysicalMemory.Available < sys.PhysicalMemory.Total * 0.2)
           close(f)
           error(['Error: Not enough RAM available.'])
        end
        
        % read reduced sweeps
        if numReducedSweeps ~= 0
           for j = 0 : numReducedSweeps - 1
                numRSweeps = uint64(0);
                sweep = libstruct('DMDReader_SWEEP');
                p_sweep = libpointer('DMDReader_SWEEP', sweep);
                [DMDReader_ErrorCode, channel_handle, sweep, ~] = calllib('dmdlib', 'DMDReader_GetReducedSweeps', channel_handle, j, 1, p_sweep.Value, numRSweeps);
                infoReducedSweep(idx+1).channels(j+1).reducedSweeps = sweep;
            end % for j numReducedSweeps
            if (DMDReader_ErrorCode ~= 0)
               close(f)
               error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetReducedSweeps'])
            end
            [~,sys] = memory;
            if (sys.PhysicalMemory.Available < sys.PhysicalMemory.Total * 0.2)
               close(f)
               error('Error: Not enough RAM available.')
            end
            readReducedValues(idx, infoReducedSweep, numReducedSweeps, channel_handle, channelInfo);
            output1 = infoReducedSweep;
            output2 = numReducedSweeps;
        elseif numReducedSweeps == 0
            output1 = [];
            output2 = [];
        end
        
   else
       close(f)
       error('Wrong type entered. Type 1 for scaled, type 2 for reduced sweep.')
   end
end

