Skip to content

Commit

Permalink
Add important error checks to qc.load_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
pcerf authored and terrorfisch committed Jun 13, 2018
1 parent 69ed1db commit 30faa75
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions MATLAB/+qc/load_dict.m
@@ -1,4 +1,4 @@
function dict_string_or_struct = load_dict(dict_string_or_struct)
function dict_string_or_struct = load_dict(dict_string_or_struct, create_dict)
% Load dict if d is a string. Otherwise leave d untouched.
%
% Important: this does not (re)load a dict if the passed in variable is
Expand All @@ -12,6 +12,10 @@
global plsdata
delim = '___';

if nargin < 2 || isempty(create_dict)
create_dict = false;
end

if ischar(dict_string_or_struct)
dict_string_or_struct = strsplit(dict_string_or_struct, ' ');

Expand All @@ -27,8 +31,14 @@
text = fileread(file_name);
dict_string_or_struct = jsondecode(text);
dict_string_or_struct = qc.array2row(dict_string_or_struct);
elseif create_dict
if strcmp(suffix, '')
dict_string_or_struct = struct(strcat('dict', delim, 'name'), dict_string_or_struct, 'global', struct());
else
error('Cannot create dictionary ''%s %s'' since it contains a space', dict_string_or_struct, suffix(2:end));
end
else
dict_string_or_struct = struct(strcat('dict', delim, 'name'), dict_string_or_struct, 'global', struct());
error('Dictionary ''%s'' does not exist', dict_string_or_struct);
end

if ~strcmp(suffix, '')
Expand Down

0 comments on commit 30faa75

Please sign in to comment.