2012-01-01 20:14:18 +01:00
|
|
|
function [] = OPLogConvert(varargin)
|
2011-11-09 19:49:50 +01:00
|
|
|
%% Define indices and arrays of structures to hold data
|
|
|
|
% THIS FILE IS AUTOMATICALLY GENERATED.
|
2012-01-01 20:14:18 +01:00
|
|
|
|
|
|
|
outputType='mat'; %Default output is a .mat file
|
2011-11-09 19:49:50 +01:00
|
|
|
|
2011-12-16 22:26:56 +01:00
|
|
|
if nargin==0
|
|
|
|
%%
|
|
|
|
if (exist('uigetfile'))
|
2012-01-01 20:14:18 +01:00
|
|
|
[FileName, PathName]=uigetfile('*.opl');
|
2011-12-16 22:26:56 +01:00
|
|
|
logfile=fullfile(PathName, FileName);
|
|
|
|
|
|
|
|
else
|
|
|
|
error('Your technical computing program does not support file choosers. Please input the file name in the argument. ')
|
2012-01-01 20:14:18 +01:00
|
|
|
end
|
|
|
|
elseif nargin>0
|
|
|
|
logfile=varargin{1};
|
|
|
|
if nargin>1
|
|
|
|
outputType=varargin{2};
|
2011-12-16 22:26:56 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-01 20:14:18 +01:00
|
|
|
if ~strcmpi(outputType,'mat') && ~strcmpi(outputType,'csv')
|
|
|
|
error('Incorrect file format specified. Second argument must be ''mat'' or ''csv''.');
|
|
|
|
end
|
|
|
|
|
|
|
|
$(ALLOCATIONCODE)
|
|
|
|
|
|
|
|
|
2011-11-09 19:49:50 +01:00
|
|
|
fid = fopen(logfile);
|
2012-01-01 20:14:18 +01:00
|
|
|
correctMsgByte=hex2dec('20');
|
|
|
|
correctSyncByte=hex2dec('3C');
|
2011-11-09 19:49:50 +01:00
|
|
|
|
|
|
|
% Parse log file, entry by entry
|
|
|
|
while (1)
|
|
|
|
%% Read logging header
|
2012-01-01 20:14:18 +01:00
|
|
|
timestamp = fread(fid, 1, '*uint32');
|
2011-11-09 19:49:50 +01:00
|
|
|
if (feof(fid)); break; end
|
2012-01-01 20:14:18 +01:00
|
|
|
datasize = fread(fid, 1, '*int64');
|
2011-11-09 19:49:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
%% Read message header
|
|
|
|
% get sync field (0x3C, 1 byte)
|
|
|
|
sync = fread(fid, 1, 'uint8');
|
2012-01-01 20:14:18 +01:00
|
|
|
if sync ~= correctSyncByte
|
2011-11-09 19:49:50 +01:00
|
|
|
disp ('Wrong sync byte');
|
|
|
|
return
|
|
|
|
end
|
|
|
|
% get msg type (quint8 1 byte ) should be 0x20, ignore the rest?
|
|
|
|
msgType = fread(fid, 1, 'uint8');
|
2012-01-01 20:14:18 +01:00
|
|
|
if msgType ~= correctMsgByte
|
2011-11-09 19:49:50 +01:00
|
|
|
disp ('Wrong msgType');
|
|
|
|
return
|
|
|
|
end
|
|
|
|
% get msg size (quint16 2 bytes) excludes crc, include msg header and data payload
|
|
|
|
msgSize = fread(fid, 1, 'uint16');
|
|
|
|
% get obj id (quint32 4 bytes)
|
|
|
|
objID = fread(fid, 1, 'uint32');
|
|
|
|
|
2012-01-01 20:14:18 +01:00
|
|
|
if (isempty(objID)) %End of file
|
|
|
|
break;
|
|
|
|
end
|
2011-11-09 19:49:50 +01:00
|
|
|
|
|
|
|
%% Read object
|
|
|
|
switch objID
|
|
|
|
$(SWITCHCODE)
|
|
|
|
otherwise
|
2012-01-01 20:14:18 +01:00
|
|
|
disp(['Unknown object ID: 0x' dec2hex(objID)]);
|
2011-11-09 19:49:50 +01:00
|
|
|
msgBytesLeft = datasize - 1 - 1 - 2 - 4;
|
|
|
|
fread(fid, msgBytesLeft, 'uint8');
|
|
|
|
end
|
|
|
|
|
2010-12-13 04:03:24 +01:00
|
|
|
end
|
|
|
|
|
2011-11-09 19:49:50 +01:00
|
|
|
%% Clean Up and Save mat file
|
|
|
|
fclose(fid);
|
|
|
|
|
2011-12-16 22:26:56 +01:00
|
|
|
% Trim output structs
|
|
|
|
$(CLEANUPCODE)
|
|
|
|
|
2012-01-01 20:14:18 +01:00
|
|
|
if strcmpi(outputType,'mat')
|
|
|
|
matfile = strrep(logfile,'opl','mat');
|
|
|
|
save(matfile $(SAVEOBJECTSCODE));
|
|
|
|
else
|
|
|
|
$(EXPORTCSVCODE);
|
|
|
|
end
|
2011-11-09 19:49:50 +01:00
|
|
|
|
|
|
|
|
2010-12-13 04:03:24 +01:00
|
|
|
|
|
|
|
%% Object reading functions
|
|
|
|
$(FUNCTIONSCODE)
|
|
|
|
|
2012-01-01 20:14:18 +01:00
|
|
|
% This function prunes the excess pre-allocated space
|
|
|
|
function [structOut]=PruneStructOfArrays(structIn, lastIndex)
|
|
|
|
|
|
|
|
fieldNames = fieldnames(structIn);
|
|
|
|
for i=1:length(fieldNames)
|
|
|
|
structOut.(fieldNames{i})=structIn.(fieldNames{i})(1:lastIndex);
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function OPLog2csv(structIn, structName, logfile)
|
|
|
|
%Get each field name from the structure
|
|
|
|
fieldNames = fieldnames(structIn);
|
|
|
|
|
|
|
|
%Create a text string with the field names
|
|
|
|
headerOut=sprintf('%s,',fieldNames{:});
|
|
|
|
headerOut=headerOut(1:end-1); %Trim off last `,` and `\t`
|
|
|
|
|
|
|
|
%Assign the structure arrays to a matrix.
|
|
|
|
matOut=zeros(max(size(structIn.(fieldNames{1}))), length(fieldNames));
|
|
|
|
|
|
|
|
if isempty(structIn.(fieldNames{1}));
|
|
|
|
matOut=[];
|
|
|
|
else
|
|
|
|
for i=1:length(fieldNames)
|
|
|
|
matOut(:,i)=structIn.(fieldNames{i});
|
|
|
|
end
|
|
|
|
end
|
|
|
|
% Create filename by replacing opl by csv
|
|
|
|
[path, name] = fileparts(logfile);
|
|
|
|
csvDirName=[name '_csv'];
|
|
|
|
[dummyA, dummyB]=mkdir(fullfile(path, csvDirName)); %Dummy outputs so the program doens't throw warnings about "Directory already exists"
|
|
|
|
csvfile=fullfile(path, csvDirName , [name '.csv']);
|
|
|
|
|
|
|
|
%Write to csv.
|
|
|
|
dlmwrite(csvfile, headerOut, '');
|
|
|
|
dlmwrite(csvfile, matOut, '-append');
|
2010-12-13 04:03:24 +01:00
|
|
|
|