Saturday, June 25, 2011

Matlab logfiles

function WriteLog(Data,cmd)
persistent FID

% Open the file
if nargin > 1
if strcmp(cmd, 'open')
date_now = clock;
date_now = strcat(num2str(date_now(1)),'_',num2str(date_now(2)),'_', num2str(date_now(3)));
FID = fopen(strcat('NCR_', date_now,'.log'), 'a');
if FID < 0
error('Cannot open file');
else
fprintf(FID, '%s\n', strcat(timestring,' openfile ---------------------------------------'));
end
return;
elseif strcmp(cmd, 'close') && FID ~= -1
fprintf(FID, '%s\n', strcat(timestring,' closefile---------------------------------------'));
fclose(FID);
FID = -1;
end
else
fprintf(FID, '%s\n', Data);
% also write to console
fprintf('%s\n', Data);
end



function s = timestring ( )
t = now;
c = datevec ( t );
s = datestr ( c, 0 );
end

No comments:

Post a Comment