PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/utility/write_pc6_mat.m

http://acq3.googlecode.com/
MATLAB | 321 lines | 269 code | 25 blank | 27 comment | 3 complexity | b4cacfe95eb82fc3170ac08e73cf7668 MD5 | raw file
  1. function [err] = write_pc6_mat(filename, dfile, data)
  2. % write_pc6_mat: write a gapfree record in pClamp6 format (abf files).
  3. %
  4. % We use initpc6header to get the base information to populate dfile.
  5. % We then write it
  6. %
  7. err = 1;
  8. if(nargin ~= 3)
  9. fprintf(1, 'write_acq_mat: requires exactly 3 arguments\n');
  10. return;
  11. end;
  12. day = clock;
  13. startdate = mod(day(1),100)*10000+day(2)*100+day(3); % in yymmdd integer format
  14. starttime = day(4)*60*60+day(5)*60+day(6); % seconds past midnight
  15. [path, fname, ext, ver] = fileparts(filename);
  16. dfile.fullfile = filename;
  17. dfile.filename = fname;
  18. dfile.path = path;
  19. dfile.ext = ext;
  20. fid = fopen(filename, 'w');
  21. if(isempty(fid) | fid < 0)
  22. return;
  23. end;
  24. pc6h = initpc6header; % create a default (blank) header
  25. % now copy from our datac dfile structure into the header
  26. pc6h.lActualEpisodes = dfile.records_in_file;
  27. pc6h.lActualAcqLength=dfile.nr_points;
  28. pc6h.fSampleRate = dfile.rate(1);
  29. pc6h.sFileComment = sprintf('%-56s', dfile.comment');
  30. pc6h.nDataFormat = 0;
  31. % pc6h.lSamplesPerTrace = dfile.nr_points;
  32. dfile.mode = 6; % corresponds to ABF file...
  33. switch(dfile.dmode)
  34. case 'VC'
  35. pc6h.nExperimentType = 1;
  36. case 'CC'
  37. pc6h.nExperimentType = 2;
  38. otherwise
  39. pc6h.nExperimentType = 1;
  40. end;
  41. pc6h.fADCSampleInterval = dfile.rate(1);
  42. dfile.record = 1;
  43. pc6h.nADCNumChannels = dfile.nr_channel;
  44. pc6h.fInstrumentScaleFactor = dfile.vgain*ones(1,16)';
  45. pc6h.lDataSectionPtr = 8; % put data 8 blocks out...
  46. % now write the header line by line.
  47. if fid == -1, % Check for invalid fid.
  48. error('write_pc6_mat: Invalid fid.');
  49. end
  50. fseek(fid,0,'bof'); % Set the pointer to beginning of the file.
  51. pc6h.lFileStartDate = startdate;
  52. pc6h.lFileStartTime = starttime;
  53. % file ID and size information
  54. fwrite(fid, pc6h.sFileType, 'schar'); % pointing to byte 0 ('ABF')
  55. fwrite(fid, pc6h.fFileVersionNumber, 'float32')% 4 (1.5 for current file)
  56. fwrite(fid, pc6h.nOperationMode, 'int16'); % 8 (3 = gapfree).
  57. fwrite(fid, pc6h.lActualAcqLength, 'int32'); % 10 (total ADC samples in file)
  58. fwrite(fid, pc6h.nNumPointsIgnored, 'short'); % 14 (0)
  59. fwrite(fid, pc6h.lActualEpisodes, 'int32'); % 16 (1 for gapfree, otherwise # sweeps)
  60. fwrite(fid, pc6h.lFileStartDate, 'int32'); % 20 (yymmdd)
  61. fwrite(fid, pc6h.lFileStartTime, 'int32'); % 24 (seconds past midnight)
  62. fwrite(fid, pc6h.lStopwatchTime, 'int32'); % 28 (0)
  63. fwrite(fid, pc6h.fHeaderVersionNumber, 'float32'); % 32 (1.5)
  64. fwrite(fid, pc6h.nFileType, 'int16'); % 36 (1 = ABF)
  65. fwrite(fid, pc6h.nMSBinFormat, 'int16'); % 38 (0 for IEEE format...)
  66. % file structure
  67. fwrite(fid, pc6h.lDataSectionPtr, 'int32'); % 40 (block number of DATA section)
  68. fwrite(fid, pc6h.lTagSectionPtr, 'int32'); % 44 (block number of Tag section)
  69. fwrite(fid, pc6h.lNumTagEntries, 'int32'); % 48 (Number of Tag entries)
  70. fwrite(fid, pc6h.lLongDescriptionPtr, 'int32'); % 52 (Block number of Scope Config Pointer)
  71. fwrite(fid, pc6h.lLongDescriptionLines, 'int32'); % 56 (Number of Scope Config sections)
  72. fwrite(fid, pc6h.lDACFilePtr, 'int32'); % 60 (block number of start of DAC section)
  73. fwrite(fid, pc6h.lDACFileNumEpisodes, 'int32'); % 64 (number of sweeps in DAC sectoin)
  74. fwrite(fid, pc6h.sUnused68, 'schar'); % 4char % 68 (unused)
  75. fwrite(fid, pc6h.lDeltaArrayPtr, 'int32'); % 72 (block number of Delta array section)
  76. fwrite(fid, pc6h.lNumDeltas, 'int32'); % 76 (number of entries in Delta array section)
  77. fwrite(fid, pc6h.lNotebookPtr, 'int32'); % 80 (block number of voice tag section)
  78. fwrite(fid, pc6h.lNotebookManEntries, 'int32'); % 84 (number of voice tag entries)
  79. fwrite(fid, pc6h.lNotebookAutoEntries, 'int32'); % 88 (unused)
  80. fwrite(fid, pc6h.lSynchArrayPtr, 'int32'); % 92 (block number of synch array section_
  81. fwrite(fid, pc6h.lSynchArraySize, 'int32'); % 96 (number of PAIRS of entries in synch array section)
  82. fwrite(fid, pc6h.nDataFormat, 'int16'); % 100 (data representation: 0 = 2-byte integer, 1 = IEEE 4 byte float)
  83. fwrite(fid, pc6h.nSimultaneousScan, 'int16'); % 102 (ADC channel scanning mode 0=mux, 1=simul; NOT IMPLEMENTED)
  84. fwrite(fid, pc6h.sUnused102, 'schar'); % 18char % 102 (
  85. % Trial hierarhcy information
  86. fwrite(fid, pc6h.nADCNumChannels, 'int16'); % 120 (number of analog input channels sampled)
  87. fwrite(fid, pc6h.fADCSampleInterval, 'float32'); % 122 (interval between multiplexed channels)
  88. fwrite(fid, pc6h.fADCSecondSampleInterval, 'float32'); % % 126 (when switching clocks)
  89. fwrite(fid, pc6h.fSynchTimeUnit, 'float32'); % 130 (synch array time unit: 0 = samples, nn = usec. normally 0)
  90. fwrite(fid, pc6h.fSecondsPerRun, 'float32'); % 134 (gapfree: duration of acq; otherwise set to -1 (ignore and refer to lEpisodesPerRun))
  91. fwrite(fid, pc6h.lNumSamplesPerEpisode, 'int32'); % 138 (# muxed ADC samples per sweep if mode is 2, 4 or 5; undefined if mode is 1 or 3)
  92. fwrite(fid, pc6h.lPreTriggerSamples, 'int32'); % 142 (pretrigger intervals = 0)
  93. fwrite(fid, pc6h.lEpisodesPerRun, 'int32'); % 146 (sweeps per run. if gapfree, set to 1.
  94. fwrite(fid, pc6h.lRunsPerTrial, 'int32'); % 150 (runs per trial. if gapfree, set to 1.
  95. fwrite(fid, pc6h.lNumberOfTrials, 'int32'); % 154 (set to 1)
  96. fwrite(fid, pc6h.nAveragingMode, 'int16'); % 158 (0 = no averaging)
  97. fwrite(fid, pc6h.nUndoRunCount, 'int16'); % 160 set to -1
  98. fwrite(fid, pc6h.nFirstEpisodeInRun, 'int16'); % 162 (set to 1)
  99. fwrite(fid, pc6h.fTriggerThreshold, 'float32'); % 164 (user units: set to 0)
  100. fwrite(fid, pc6h.nTriggerSource, 'int16'); % 168 (set to -1 for external, > 0 for physical channel number, -3 for start-to-start interval)
  101. fwrite(fid, pc6h.nTriggerAction, 'int16'); % 170 (0 = one sweep, 1 = one run, 2 = one trial)
  102. fwrite(fid, pc6h.nTriggerPolarity, 'int16'); % 172 (0 = rising, 1 == falling)
  103. fwrite(fid, pc6h.fScopeOutputInterval, 'float32'); % 174 (set to 0)
  104. fwrite(fid, pc6h.fEpisodeStartToStart, 'float32'); % 178 (time between start of sweeps, in seconds = cycle time)
  105. fwrite(fid, pc6h.fRunStartToStart, 'float32'); % 182 (time between start of runs (seconds)
  106. fwrite(fid, pc6h.fTrialStartToStart, 'float32'); % 186 (time between trials (seconds)
  107. fwrite(fid, pc6h.lAverageCount, 'int32'); % 190 (number of runs for each average: set to 1)
  108. fwrite(fid, pc6h.lClockChange, 'int32'); % 194 (# samples after which second sampling interval begins: set to -1)
  109. fwrite(fid, pc6h.nAutoTriggerStrategy, 'int16'); % 2char % 198 (0 = do not auto trig; 1 = autotrig; only when nOpmode = highspeed osc).
  110. % display parameters
  111. fwrite(fid, pc6h.nDrawingStrategy, 'int16'); % 200 (0 = not draw, 1 = update immed.... set to 0)
  112. fwrite(fid, pc6h.nTiledDisplay, 'int16'); % 202 (set to 0)
  113. fwrite(fid, pc6h.nEraseStrategy, 'int16'); % 204 (set to 2)
  114. fwrite(fid, pc6h.nDataDisplayMode, 'int16'); % 206 (set to 1 = lines)
  115. fwrite(fid, pc6h.lDisplayAverageUpdate, 'int32'); % 208 (set to -1)
  116. fwrite(fid, pc6h.nChannelStatsStrategy, 'int16'); % 212 (set to 0)
  117. fwrite(fid, pc6h.lCalculationPeriod, 'int32'); % 214 (set to 16384)
  118. fwrite(fid, pc6h.lSamplesPerTrace, 'int32'); % 218 (number of multiplexed adc samples displayed per trace)
  119. fwrite(fid, pc6h.lStartDisplayNum, 'int32'); % 222 (set to 1)
  120. fwrite(fid, pc6h.lFinishDisplayNum, 'int32'); % 226 (set to 0 to finish at end of sweep)
  121. fwrite(fid, pc6h.nMultiColor, 'int16'); % 230 (set to 1 for multicolor)
  122. fwrite(fid, pc6h.nShowPNRawData, 'int16'); % 232 (set to 0 for display corrected data
  123. fwrite(fid, pc6h.fStatisticsPeriod, 'float32'); % 234 set to 0
  124. fwrite(fid, pc6h.lStatisticsMeasurements, 'int32'); % 238 set to 0 ***** possible problem *****
  125. fwrite(fid, pc6h.nstatisticsSaveStrategy, 'int16'); % 242 set to 0 for auto
  126. % hardware information
  127. fwrite(fid, pc6h.fADCRange, 'float32'); % 244 ADC-full scale (5 or 10 V)
  128. fwrite(fid, pc6h.fDACRange, 'float32'); % 248 DAC full scale (10V)
  129. fwrite(fid, pc6h.lADCResolution, 'int32'); % 252 number of adc counts corresponding to full scale voltage: 2048 or 16384
  130. fwrite(fid, pc6h.lDACResolution, 'int32'); % 256 dac counts for full scale....
  131. % Environment information
  132. fwrite(fid, pc6h.nExperimentType, 'int16'); % 260 % 0 for voltage clamp, 1 for current clamp
  133. fwrite(fid, pc6h.nAutosampleEnable, 'int16'); % 262 % set to 0 (disabled)
  134. fwrite(fid, pc6h.nAutosampleADCNum, 'int16'); % 264 % physical sample number for autosample: set to 1
  135. fwrite(fid, pc6h.nAutosampleInstrument, 'int16'); % 266 % instrument: set to 0 (unknown)
  136. fwrite(fid, pc6h.fAutosampleAdditGain, 'float32'); % 268 % additional gain multiplier (set to gain reading or 1)
  137. fwrite(fid, pc6h.fAutosampleFilter, 'float32'); % 272 % low pass filter (set to LPF reading or 100000)
  138. fwrite(fid, pc6h.fAutosampleMembraneCapacitance, 'float32'); % 276 cap : set to 0 (not read)
  139. fwrite(fid, pc6h.nManualInfoStrategy, 'int16'); % 280 set to 0.
  140. fwrite(fid, pc6h.fCellID1, 'float32'); % 282 numeric cell identifier (set to 1)
  141. fwrite(fid, pc6h.fCellID2, 'float32'); % 286 numeric identifier (set to 0)
  142. fwrite(fid, pc6h.fCellID3, 'float32'); % 290 numeric identifier (set to 0)
  143. fwrite(fid, pc6h.sCreatorInfo, 'schar'); % 16char % 294 write as "Clampex 6.0"
  144. fwrite(fid, pc6h.sFileComment, 'schar'); % 56char % 310 56 byte ascii comment string (copy comment here)
  145. fwrite(fid, pc6h.sUnused366, 'schar'); % 12char % 366 unused
  146. % Multichannel information
  147. fwrite(fid, pc6h.nADCPtoLChannelMap, 'int16'); % 378 physical to logical channel map (end with -1 if fewer than 16)
  148. fwrite(fid, pc6h.nADCSamplingSeq, 'int16'); % 410 ADC sampling sequence (pad with -1 if fewer than 16)
  149. fwrite(fid, pc6h.sADCChannelName, 'char'); % 442 channel names (set to spaces)
  150. fwrite(fid, pc6h.sADCUnits, 'char'); % 8char % 602 user units for adc (set to spaces)
  151. fwrite(fid, pc6h.fADCProgrammableGain, 'float32'); % 730 adc programmable gain (read it or set to 1)
  152. fwrite(fid, pc6h.fADCDisplayAmplification, 'float32'); % 794 (set to 1: array)
  153. fwrite(fid, pc6h.fADCDisplayOffset, 'float32'); % 858 (set to 0: array)
  154. fwrite(fid, pc6h.fInstrumentScaleFactor, 'float32'); % 922 (set to 1: array)
  155. fwrite(fid, pc6h.fInstrumentOffset, 'float32'); % 986 (set to 0 array)
  156. fwrite(fid, pc6h.fSignalGain, 'float32'); % 1050 (set to 1 array)
  157. fwrite(fid, pc6h.fSignalOffset, 'float32'); % 1114 (set to 0 array)
  158. fwrite(fid, pc6h.fSignalLowpassFilter, 'float32'); % 1178 (set to 100000 array)
  159. fwrite(fid, pc6h.fSignalHighpassFilter, 'float32'); % 1242 (set to 0 array)
  160. fwrite(fid, pc6h.sDACChannelName, 'char'); % 1306 (set to spaces)
  161. fwrite(fid, pc6h.sDACChannelUnits, 'char'); % 8char % 1346 (set to spaces)
  162. fwrite(fid, pc6h.fDACScaleFactor, 'float32'); % 1378 (dac channel gains: set to 1)
  163. fwrite(fid, pc6h.fDACHoldingLevel, 'float32'); % 1394 (set to 0)
  164. fwrite(fid, pc6h.nSignalType, 'int16'); % 12char % 1410 (set to 0 for no signal conditioner)
  165. fwrite(fid, pc6h.sUnused1412, 'char'); % 10char % 1412
  166. % synchronous timer outputs
  167. fwrite(fid, pc6h.nOUTEnable, 'int16'); % 1422 % set to 0
  168. fwrite(fid, pc6h.nSampleNumberOUT1, 'int16'); % 1424 % set to 0
  169. fwrite(fid, pc6h.nSampleNumberOUT2, 'int16'); % 1426 % set to 0
  170. fwrite(fid, pc6h.nFirstEpisodeOUT, 'int16'); % 1428 % set to 0
  171. fwrite(fid, pc6h.nLastEpisodeOUT, 'int16'); % 1430 % set to 0
  172. fwrite(fid, pc6h.nPulseSamplesOUT1, 'int16'); % 1432 % set to 0
  173. fwrite(fid, pc6h.nPulseSamplesOUT2, 'int16'); % 1434 % set to 0
  174. %Epoch waveform and pulses
  175. fwrite(fid, pc6h.nDigitalEnable, 'int16'); % 1436 % set to 0
  176. fwrite(fid, pc6h.nWaveformSource, 'int16'); % 1438 % set to 0
  177. fwrite(fid, pc6h.nActiveDACChannel, 'int16'); % 1440 % set to 0
  178. fwrite(fid, pc6h.nInterEpisodeLevel, 'int16'); % 1442 % set to 0
  179. fwrite(fid, pc6h.nEpochType, 'int16'); % 1444 % set to 0
  180. fwrite(fid, pc6h.fEpochInitLevel, 'float32'); % 1464 % set to 0
  181. fwrite(fid, pc6h.fEpochLevelInc, 'float32'); % 1504 % set to 0
  182. fwrite(fid, pc6h.nEpochInitDuration, 'int16'); % 1544 % set to 0
  183. fwrite(fid, pc6h.nEpochDurationInc, 'int16'); % 1564 % set to 0
  184. fwrite(fid, pc6h.nDigitalHolding, 'int16'); % 1584 % set to 0
  185. fwrite(fid, pc6h.nDigitalInterEpisode, 'int16'); % 1586 % set to 0
  186. fwrite(fid, pc6h.nDigitalValue, 'int16'); % 1588 ^% set to 0
  187. fwrite(fid, pc6h.fWaveformOffset, 'float32'); % 1608 % set to 0
  188. fwrite(fid, pc6h.sUnused1612, 'schar'); % 8char % 1612
  189. % DAC output file
  190. fwrite(fid, pc6h.fDACFileScale, 'float32'); % 1620 % set to 1
  191. fwrite(fid, pc6h.fDACFileOffset, 'float32'); % 1624 % set to 0
  192. fwrite(fid, pc6h.sUnused1628, 'schar'); % 2char % 1628 % unused
  193. fwrite(fid, pc6h.nDACFileEpisodeNum, 'int16'); % 1630 set to 0
  194. fwrite(fid, pc6h.nDACFileADCNum, 'int16'); % 1632 set to 0
  195. fwrite(fid, pc6h.sDACFileName, 'schar'); % 12char % 1634 % set to spaces
  196. fwrite(fid, pc6h.sDACFilePath, 'schar'); % 60char % 1646 set to spaces
  197. fwrite(fid, pc6h.sUnused1706, 'schar'); % 12char % 1706
  198. % Conditioning pulse train
  199. fwrite(fid, pc6h.nConditEnable, 'int16'); % 1718 % set to 0
  200. fwrite(fid, pc6h.nConditChannel, 'int16'); % 1720 0
  201. fwrite(fid, pc6h.lConditNumPulses, 'int32'); % 1722 0
  202. fwrite(fid, pc6h.fBaselineDuration, 'float32'); % 1726 0
  203. fwrite(fid, pc6h.fBaselineLevel, 'float32'); % 1730 0
  204. fwrite(fid, pc6h.fStepDuration, 'float32'); % 1734 0
  205. fwrite(fid, pc6h.fStepLevel, 'float32'); % 1738 0
  206. fwrite(fid, pc6h.fPostTrainPeriod, 'float32'); % 1742 0
  207. fwrite(fid, pc6h.fPostTrainLevel, 'float32'); % 1746 0
  208. fwrite(fid, pc6h.sUnused1750, 'schar'); % 12char % 1750 0
  209. % variable parameter user list
  210. fwrite(fid, pc6h.nParamToVary, 'int16'); % 1762 % set to 0
  211. fwrite(fid, pc6h.sParamValueList, 'schar'); % 80char % 1764 % set to 0
  212. % statistics measurement
  213. fwrite(fid, pc6h.nAutopeakEnable, 'int16'); % 1844 % set to 0
  214. fwrite(fid, pc6h.nAutopeakPolarity, 'int16'); % 1846 % set to 0
  215. fwrite(fid, pc6h.nAutopeakADCNum, 'int16'); % 1848 % set to 0
  216. fwrite(fid, pc6h.nAutopeakSearchMode, 'int16'); % 1850 % set to -1
  217. fwrite(fid, pc6h.lAutopeakStart, 'int32'); % 1852 % set to 0
  218. fwrite(fid, pc6h.lAutopeakEnd, 'int32'); % 1856 % set to 0
  219. fwrite(fid, pc6h.nAutopeakSmoothing, 'int16'); % 1860 % set to 3
  220. fwrite(fid, pc6h.nAutopeakBaseline, 'int16'); % 1862 % set to =2
  221. fwrite(fid, pc6h.nAutopeakAverage, 'int16'); % 1864 % set to 0
  222. fwrite(fid, pc6h.sUnused1866, 'schar'); % 14char % 1866
  223. % Channel arithmetic
  224. fwrite(fid, pc6h.nArithmeticEnable, 'int16'); % 1880 % % set to 0
  225. fwrite(fid, pc6h.fArithmeticUpperLimit, 'float32'); % 1882 0
  226. fwrite(fid, pc6h.fArithmeticLowerLimit, 'float32'); % 1886 0
  227. fwrite(fid, pc6h.nArithmeticADCNumA, 'int16'); % 1890 0
  228. fwrite(fid, pc6h.nArithmeticADCNumB, 'int16'); % 1892 0
  229. fwrite(fid, pc6h.fArithmeticK1, 'float32'); % 1894 0
  230. fwrite(fid, pc6h.fArithmeticK2, 'float32'); % 1898 0
  231. fwrite(fid, pc6h.fArithmeticK3, 'float32'); % 1902 0
  232. fwrite(fid, pc6h.fArithmeticK4, 'float32'); % 1906 0
  233. fwrite(fid, pc6h.sArithmeticOperator, 'schar'); % 2char % 1910 '+'
  234. fwrite(fid, pc6h.sArithmeticUnits, 'schar'); % 8char % 1912 % spaces
  235. fwrite(fid, pc6h.fArithmeticK5, 'float32'); % 1920 0
  236. fwrite(fid, pc6h.fArithmeticK6, 'float32'); % 1924 0
  237. fwrite(fid, pc6h.nArithmeticExpression, 'int16'); % 1928 set to 1
  238. fwrite(fid, pc6h.sUnused1930, 'schar'); % 2char % 1930
  239. % Online subtraction
  240. fwrite(fid, pc6h.nPNEnable, 'int16'); % 1932 % set to 0
  241. fwrite(fid, pc6h.nPNPosition, 'int16'); % 1934 % set to 0
  242. fwrite(fid, pc6h.nPNPolarity, 'int16'); % 1936 % set to -1
  243. fwrite(fid, pc6h.nPNNumPulses, 'int16'); % 1938 set to 1
  244. fwrite(fid, pc6h.nPNADCNum, 'int16'); % 1940 set to 1
  245. fwrite(fid, pc6h.fPNHoldingLevel, 'float32'); % 1942 set to 0
  246. fwrite(fid, pc6h.fPNSettlingTime, 'float32'); % 1946 set to 1
  247. fwrite(fid, pc6h.fPNInterpulse, 'float32'); % 1950 set to 1
  248. fwrite(fid, pc6h.sUnused1954, 'schar'); % 12char % 1954
  249. % Miscellaneous elements placed in "unused space at end of header block"
  250. fwrite(fid, pc6h.nListEnable, 'int16'); % 1966 set to 0
  251. fwrite(fid, pc6h.sUnused1966, 'schar'); % 80char % 1968 set to 0's
  252. % now write the data. The data is stored 8 blocks out, as a continuous.
  253. datafmt=pc6h.nDataFormat;
  254. cn = pc6h.nAutosampleADCNum + 1;
  255. pc6h.fGain=pc6h.fInstrumentScaleFactor.*pc6h.fAutosampleAdditGain;
  256. pc6h.fGain=pc6h.fGain'.*pc6h.fADCProgrammableGain.*pc6h.fSignalGain;
  257. gain = pc6h.fADCRange / pc6h.lADCResolution / (pc6h.fInstrumentScaleFactor(cn) * pc6h.fAutosampleAdditGain * pc6h.fADCProgrammableGain(cn) * pc6h.fSignalGain(cn)) ;
  258. offset = pc6h.fSignalOffset(cn);
  259. ns = pc6h.lActualAcqLength / pc6h.lActualEpisodes;
  260. dpos = (pc6h.lDataSectionPtr-1) * 512;
  261. % can't position past end of currently written file, so
  262. % we must fill the space in between with empty...
  263. cpos = ftell(fid);
  264. if(cpos < dpos)
  265. filler = zeros(1, dpos-cpos+1);
  266. fwrite(fid, filler, 'schar');
  267. end;
  268. % now we can write.
  269. chunksize = 512; % gapfree chunksize is 8192 bytes.
  270. en = 1;
  271. switch(datafmt)
  272. case 0 % 2 byte integer format.
  273. fseek(fid, dpos, 'bof');
  274. data = 0.5 + (data - offset) / gain;
  275. fwrite(fid, data, 'int16');
  276. leftover = rem(length(data)*2, chunksize); % left over bytes (int 16 writes...)
  277. za = zeros(1, length(leftover));
  278. fwrite(fid, za, 'int16'); % fill with zeros
  279. case 1 % 4 byte floating format
  280. fseek(fid, dpos + (en-1) * ns, 'bof');
  281. fwrite(fid, data, 'float32');
  282. leftover = rem(length(data)*4, chunksize); % left over bytes.
  283. za = zeros(1, length(leftover));
  284. fwrite(fid, za, 'int16'); % fill with zeros.
  285. otherwise
  286. end;
  287. filler = zeros(1, chunksize);
  288. fwrite(fid, filler, 'schar'); % this seems to be important, even if there's nothing there.
  289. % then close the file
  290. fclose(fid);
  291. err = 0; % success!
  292. return;