-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_motl_fill_field2.m
75 lines (58 loc) · 2.02 KB
/
sg_motl_fill_field2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function motl = sg_motl_fill_field2(motl,field,value)
%% sg_motl_fill_field2
% A function to fill a type-2 motl field with given values. The values must
% either have the same dimensions of the motl or have a single value. In
% the case of a single value, this value as assigned to all fields.
%
% The given fields are checked against the default fields and types are
% enforced.
%
% WW 04-2021
%% Check check
n_motls = numel(motl.motl_idx);
n_values = numel(value);
if (n_values ~= n_motls) && (n_values ~= 1)
error('ACHTUNG!!! Number of values is not 1 and does not match number of motl entries!!!');
end
% Number of repeats
if n_values ~= n_motls
rep_val = n_motls;
else
rep_val = 1;
end
%% If non-cell input, parse and store
% Get fields
motl_fields = sg_get_motl_fields;
f_idx = strcmp(field,motl_fields(:,1));
switch motl_fields{f_idx,3}
case 'float'
if isnumeric(value) || islogical(value)
value = repmat(single(value),[rep_val,1]);
elseif ischar(value)
error('ACHTUNG!!! You are trying to store strings into a "float" field!!!');
end
case 'int'
if isnumeric(value) || islogical(value)
value = repmat(int32(value),[rep_val,1]);
elseif ischar(value)
error('ACHTUNG!!! You are trying to store strings into an "int" field!!!');
end
case 'str'
if isnumeric(value) || islogical(value)
value = repmat(cellstr(num2str(value(:))),[rep_val,1]);
elseif ischar(value)
if n_values ~= n_motls
value = repmat({value},[rep_val,1]);
else
value = cellstr(value(:));
end
elseif iscell(value)
num_idx = cellfun(@(x) isnumeric(x) | islogical(x),value);
if any(num_idx)
value(num_idx) = strsplit(num2str([value{num_idx}]));
end
value = repmat(value,[rep_val,1]);
end
end
% Store value
motl.(field) = value;