MATLAB: How to convert a double to a data with the following features

how can i convert a double

The data consists of two bytes per point with no characters between bytes or points. The point data is sent high byte first. The data block has a header which consists of the # character followed by several ascii coded numeric characters. The first of these defines the number of ascii characters to follow and these following characters define the length of the binary data in bytes.

Best Answer

This would get you started:
bindatalength = sprintf('%d', numel(waveform)*2); %length of binary data (2*numbers of points) encoded in ASCII.
lengthlength = sprintf('%d', numel(bindatatlength); %length of the length string, encoded in ascii , must be only one character
assert(numel(lengthlength) == 1, 'wavelength has too many points');
commandheader = sprintf('ARB %c%s', lengthlength, bindatalength);
You then need to encode the waveform itself as two byte integer in big endian form. As far as I can tell there is no information provided on how to do that. The way I'd find out what to do would be to send the command ARB1? to read a known waveform and work out the format.
Possibly, the [-1, +1] range is normalised to uint16 [0 65535], if that is the case:
waveformbinary = uint16((waveformArray + 1)*65535)
Or maybe it's converted to signed 16-bit, int16 [-32768, 32767]:
waveformbinary = int16(waveform*32767);
Or maybe it's something else. As said, ARB1? could help you figure it out.
Once you've got the above worked out:
fprintf(yourdevice, commandheader); %write as ASCII
fwrite(yourdevice, waveformbinary, 0, 'ieee-be'); %write binary data in BIG ENDIAN format (high byte first)
fprintf(yourdevice, '\n'); %terminate command