chan = 1;
mode = 'NORM';

switch chan
  case 1
    cmd = ':WAV:SOUR CHAN1';
  case 2
    cmd = ':WAV:SOUR CHAN2';
  case 3
    cmd = ':WAV:SOUR CHAN3';
  case 4
    cmd = ':WAV:SOUR CHAN4';
 endswitch
  
usbtmc_write(scope,cmd);
usbtmc_write(scope,':WAVEFORM:FORMAT ASCII');


switch mode
  case 'NORM'
    usbtmc_write(scope,':WAVeform:POINTS:MODE NORM');
  case 'RAW'
    usbtmc_write(scope,':WAVeform:POINTS:MODE RAW');
endswitch


usbtmc_write(scope,':WAV:PRE?');
[dataFormat, nFormatRead] = usbtmc_read(scope, 200);
dataFormat = str2num(char(dataFormat));

format     = dataFormat(1);
type       = dataFormat(2);
points     = dataFormat(3);
count      = dataFormat(4);
xincrement = dataFormat(5);
xorigin    = dataFormat(6);
xreference = dataFormat(7);
yincrement = dataFormat(8);
yorigin    = dataFormat(9);
yreference = dataFormat(10);

usbtmc_write(scope,':WAV:DATA?');

% need to read 10-byte header defining data block size in bytes, plus up to
% 16 bytes for ASCII floating-point numbers...
nChar = points; %uint32(10 + 14*points) % THIS IS WHERE I WAS EXPERIMENTING.
% If nChar is made larger than 10+points, the usbtmc_read() function only returns 2 bytes.
% If nChar is made equal to 10+points, usbtmc_read() returns that many bytes, however for
% ASCII format, the actual number of bytes that must be returned is much larger (10+14*points)
[data_raw, nRead] = usbtmc_read(scope,nChar);
data_str = char(data_raw(11:length(data_raw)));

Xtime = linspace(0,xincrement*points,points);
Ydata = str2num(data_str);
Xtrigger = -xorigin;