Skip to content
Snippets Groups Projects
Commit 47e59f41 authored by leprob001's avatar leprob001
Browse files

Added routine for loading .rfb files into memory, as well as testfiles.

parents
No related branches found
No related tags found
No related merge requests found
function [datafile] = load()
%LOAD Loads a test rfb file into memory.
[currentDirectory, ~, ~] = fileparts(mfilename('fullpath'));
dataFileName = fullfile(currentDirectory, '..', 'data', '0272.rfb');
%dataFileName = fullfile(currentDirectory, '..', 'data', 'rf5331.rfb');
datafile = artoa.data.load.rfb(dataFileName);
end
This diff is collapsed.
This diff is collapsed.
function [ rfbfile ] = rfb(pFilename)
%RFB Loads a .rfb file into memory using the tomlini function.
% Creates a struct that contains the sections as properties.
%
% Parameters:
% pFilename (string) The filename to be read.
%
%% Initialize input parameter
rfbfile = false;
%% Parameter check
parameter_error = false;
if (~ischar(pFilename))
warning([mfilename ': pFilename is not a string!']);
parameter_error = true;
else
% get filename details
[pathstr, name, ext] = fileparts(pFilename);
if (~strcmp(ext, '.rfb'))
warning([mfilename ': Please check input parameter. Wrong file extension! Should be .rfb!']);
parameter_error = true;
end
end
if parameter_error
return; % return if parameter check has not been passed successfully
else
clear parameter_error;
end
rfbfile = artoa.data.load.tomlini(pFilename);
end
function [ tomlini ] = tomlini(pFilename, pReservedCharacters)
%TOMLINI Reads a file that consist of a specific combination of a ini and toml file.
% Creates a struct variable that has properties name as the sections in
% the file. The values of these properties either have the type struct,
% cell or double, depending on how the data can be parsed.
%
% Parameters:
% pFilename (string) The filename that should be read.
% pReservedCharacters (struct) Optional. It is possible to customize
% the characters that are used for parsing.
% Default values:
% section [
% sectionTitleEnd ]
% comment %
% variableIndicator -
%
%
% Example file:
%
% [SectionName]
% -var value
% -var2 value
%
% [SecondSectionName]
% 10 20 30 40
% 50 60 70 80
%
% [ThirdSection]
% sometext 10 20
% someOtherText 30 40
%% Initialize return variables
tomlini = false;
%% Initialize variables required for processing
tomlini = {};
currentSectionName = '';
% File specific characters required for processing
reservedCharacters = struct( ...
'section', '[', ... % character indicating new section
'sectionTitleEnd', ']', ... % immediately follows the section title
'comment', '%', ... % character to indicate comments
'variableIndicator', '-' ... % indicates a variable in the current line
);
%% Parameter check
parameterError = false;
if (~ischar(pFilename))
warning([mfilename ': Given parameter is not a string!']);
parameterError = true;
end
if (exist('pReservedCharacters', 'var') && isstruct(pReservedCharacters))
% merge the two structs
names = fieldnames(pReservedCharacters);
for i = 1:length(names)
reservedCharacters.(names{i}) = pReservedCharacters.(names{i});
end
end
if (parameterError)
return;
else
clear parameterError;
end
%% Read the file
fileContent = fileread(pFilename);
%% Parse the file
allSections = splitIntoSections(fileContent);
for i = 1:length(allSections)
if (isComment(allSections{i})) % ignore the line
continue;
end
[title, content] = extractTitleAndContent(allSections(i));
switch determineParseType(content)
case 'matrix'
tomlini.(title) = parseAsMatrix(content);
case 'struct'
tomlini.(title) = parseAsStruct(content);
case 'cell'
tomlini.(title) = parseAsCell(content);
otherwise
warning([mfilename ': Something went wrong during type estimation for parsing!']);
end
end
function [ type ] = determineParseType(pContent)
lines = splitlines(pContent);
% check if everything can be parsed as number
tmpRow = strsplit(lines{1}, {'\t', ' '}, 'CollapseDelimiter', true);
tmpRow = tmpRow(~cellfun('isempty', tmpRow));
nanInRow = contains(tmpRow, 'NaN');
rowAsNumber = str2double(tmpRow);
if (nanInRow == isnan(rowAsNumber))
type = 'matrix';
return;
end
% it cannot be parsed as matrix, so check if it can be parsed as
% struct
hasVariableIndicator = ...
(lines{1}(1) == reservedCharacters.variableIndicator);
alphabeticLetters = isletter(lines{1});
if hasVariableIndicator && alphabeticLetters(2)
type = 'struct';
else
type = 'cell';
end
end
function [ sections ] = splitIntoSections(pFileContent)
sections = split(pFileContent, reservedCharacters.section);
% remove all empty cells
sections = sections(~cellfun('isempty', sections));
end
function [ title, content ] = extractTitleAndContent(pSection)
parts = split(pSection, reservedCharacters.sectionTitleEnd);
title = parts{1};
content = strtrim(parts{2});
end
function [ isComment ] = isComment(pLine)
trimmedLine = strtrim(pLine);
if (trimmedLine(1) == reservedCharacters.comment)
isComment = true;
else
isComment = false;
end
end
function [ returnContent ] = parseAsStruct(pContent)
lines = splitlines(pContent);
returnContent = struct();
for i = 1:length(lines)
if (isComment(lines{i}))
continue
end
[line, comment] = extractValueAndComment(lines{i});
[key, value] = strtok(line(2:end));
% check if key already exists
if (isfield(returnContent, key))
% find key that is available
i = 1;
while (isfield(returnContent, [key num2str(i)]))
i = i + 1;
end
key = [key num2str(i)];
clear i;
end
valueAsNumber = str2num(value);
if isempty(valueAsNumber)
returnContent.(key) = strtrim(value);
else
returnContent.(key) = valueAsNumber;
end
end
end
function [ matrix ] = parseAsMatrix(pContent)
lines = splitlines(pContent);
for i = 1:length(lines)
if (isComment(lines{i}))
continue
end
% cut rest of line if a comment character is available
[line, comment] = extractValueAndComment(lines{i});
lines{i} = line;
clear line;
% split by whitespaces
lineAsCell = strsplit(lines{i}, {'\t', ' '}, 'CollapseDelimiters', true);
% remove empty cells
lineAsCell = lineAsCell(~cellfun('isempty', lineAsCell));
lineAsDouble = str2double(lineAsCell);
if any(isnan(lineAsDouble))
break;
end
if i == 1
matrix = lineAsDouble;
else
matrix = [matrix; lineAsDouble];
end
end
end
function [ cell ] = parseAsCell(pContent)
lines = splitlines(pContent);
cell = {};
for i = 1:length(lines)
if (isComment(lines{i}))
continue
end
[line, comment] = extractValueAndComment(lines{i});
cell{i} = strsplit( ...
strtrim(line), {' ', '\t'}, 'CollapseDelimiters', true ...
);
end
cell = vertcat(cell{:}); % combine all cells to one
end
function [ value, comment ] = extractValueAndComment(pString)
tmp = strsplit(pString, reservedCharacters.comment);
value = tmp{1};
if length(tmp) == 2
comment = tmp{2};
else
comment = '';
end
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment