% writes a pwl-file for PSpice
%
% file : filename for data
% time : time
% data : data
% t : time format, e.g. 'us' (s) or 'ms' ...
% d : data format, e.g. 'm', 'u', or 'n' ...
function writepwl(file, time, data, t, d)
fid = fopen(file, 'w');
% write a backet '(' - PSpice wants it
fwrite(fid, '(', 'char');
% write five pairs of time and data in one line, start next line and write
% a plus '+' sign. fprintf continues until time and data are 'empty'
fprintf(fid, ['%0.4f' t ' %0.4f' d ...
' %0.4f' t ' %0.4f' d ...
' %0.4f' t ' %0.4f' d ...
' %0.4f' t ' %0.4f' d ...
' %0.4f' t ' %0.4f' d ...
'\r\n+ '], [time'; data']);
% go four steps back in the file (that's where the carriage return '\r' is)
fseek(fid, -4, 0);
% overwrite the '\r\n' with a closing bracket ')' - PSpice needs it
fwrite(fid, [d ') '], 'char');
fid = fclose(fid);