Commit d8272a76 authored by Jonas Stienen's avatar Jonas Stienen

Fixing metadata problems for daff v17 to v15 converter

parent 1ebebe6d
function [ metadata ] = daffv15_metadata_addKey(metadata, keyname, datatype, value)
function [ metadata ] = daffv15_metadata_addKey( metadata_in, keyname, datatype, value)
%DAFF_METADATA_ADDKEY Adds another key to a DAFF metadata struct
% TODO:
% ITAToolbox schauen
......@@ -9,42 +9,54 @@ function [ metadata ] = daffv15_metadata_addKey(metadata, keyname, datatype, val
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
% if (~isempty(metadata))
% if (~isstruct(metadata)), error(['[' mfilename '] Wrong datatype for metadata']); end;
% else
metadata = struct('name', {}, 'datatype', {}, 'value', {});
% end
if (~ischar(keyname)), error(['[' mfilename '] Key name must be a string']); end;
if (~isempty(metadata_in))
if (~isstruct(metadata_in))
error( 'Input metadata is not empty and not a struct' )
end;
metadata = metadata_in;
else
metadata = struct();
end
keyname(ismember(keyname,' ')) = '_';
keyname(ismember(keyname,',.:;!')) = [];
if (~ischar(keyname))
error('Key %s name must be a string', keyname )
end;
% Keynames are case-insensitive (convert to upper case)
keyname = upper(keyname);
% Test wheather a key of the given name already exists
if any(strcmp({metadata(:).name}, keyname))
error(['[' mfilename '] Key ''' keyname ''' already exists']);
if isfield( metadata, keyname )
error(['Key ''' keyname ''' already exists']);
end
if (~ischar(datatype)), error(['[' mfilename '] Datatype must be a string']); end;
if (~ischar(datatype))
error(['Datatype must be a string'])
end
switch (upper(datatype))
case 'BOOL'
if (~islogical(value)), error(['[' mfilename '] Value must be logical for boolean keys']); end;
metadata(end+1) = struct('name', keyname, 'datatype', 0, 'value', value);
if (~islogical(value))
error(['[' mfilename '] Value must be logical for boolean keys'])
end;
metadata.(keyname) = boolean( value );
case 'INT'
if (~isfinite(value)), error(['[' mfilename '] Value must be finite']); end;
if (~isreal(value)), error(['[' mfilename '] Value must be real']); end;
if (value ~= ceil(value)), error(['[' mfilename '] Value must be an integer number for integer keys']); end;
metadata(end+1) = struct('name', keyname, 'datatype', 1, 'value', int32(value));
metadata.(keyname) = int32(value);
case 'FLOAT'
if (~isfinite(value)), error(['[' mfilename '] Value must be finite']); end;
if (~isreal(value)), error(['[' mfilename '] Value must be real']); end;
metadata(end+1) = struct('name', keyname, 'datatype', 2, 'value', double(value));
if (~isreal(value)), error(['[' mfilename '] Value must be real']); end;
metadata.(keyname) = double(value);
case 'STRING'
if (~ischar(value)), error(['[' mfilename '] Value must be a string for string keys']); end;
metadata(end+1) = struct('name', keyname, 'datatype', 3, 'value', value);
if (~ischar(value)), error(['[' mfilename '] Value must be a string for string keys']); end;
metadata.(keyname) = char(value);
end
end
......@@ -35,33 +35,42 @@ function [ ] = daffv15_write_metadata( fid, metadata )
% Logical scalar (DAFF_BOOL)
if islogical(value) && isscalar(value)
type = 'BOOL';
datatype = 0;
end
% Integer scalar (DAFF_INT)
if isinteger(value) && isfinite(value) && isreal(value) && isscalar(value)
type = 'INT';
datatype = 1;
end
% Floating point scalar (DAFF_FLOAT)
if isfloat(value) && isfinite(value) && isreal(value) && isscalar(value)
type = 'FLOAT';
datatype = 2;
end
% String (DAFF_STRING)
if ischar( value )
type = 'STRING';
datatype = 3;
end
if isempty( type )
error( 'Unsupported datatype %s for key ''%s'' in metadata', class( value ), name );
end
metadata_new_format = daffv15_metadata_addKey( metadata_new_format, name, type, value );
%metadata_new_format = daffv15_metadata_addKey( metadata_new_format, name, type, value );
metadata_new_format(i).name = name;
metadata_new_format(i).datatype = datatype;
metadata_new_format(i).value = value;
end
metadata = metadata_new_format;
end
nkeys = length( metadata );
% Number of keys
fwrite( fid, nkeys, 'int32' );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment