-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDMS_dataToGroupChanStruct_v3.m
85 lines (76 loc) · 3.04 KB
/
TDMS_dataToGroupChanStruct_v3.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
76
77
78
79
80
81
82
83
84
85
function output = TDMS_dataToGroupChanStruct_v3(inputStruct,varargin)
%TDMS_dataToGroupChanStruct_v3
%
% translates objects AND properties to valid field names
%
% Similar to TDMS_dataToGroupChanStruct_v1, but also translates
% properties
%
% output = TDMS_dataToGroupChanStruct_v3(inputStruct)
%
% OPTIONAL INPUTS (property/value pairs)
% ============================================
% REPLACE_STR = default '_', what to replace invalid characters with
% PREPEND_STR = default 'v' (for variable), gets prepended if first
% character is invalid (must be alpha, not numeric or _)
% ALWAYS_PREPEND = default false, if true always prepends the prepend string,
% regardless of whether or not it is needed
%
%
%
% See Also
% --------
% TDMS_genvarname2
% TDMS_dataToGroupChanStruct_v4
REPLACE_STR = '_';
PREPEND_STR = 'v';
ALWAYS_PREPEND = false;
for i = 1:2:length(varargin)
switch upper(varargin{i})
case 'REPLACE_STR'
REPLACE_STR = varargin{i+1};
case 'PREPEND_STR'
PREPEND_STR = varargin{i+1};
case 'ALWAYS_PREPEND'
ALWAYS_PREPEND = varargin{i+1};
otherwise
error('Unrecognized option: %s',varargin{i});
end
end
propNames = inputStruct.propNames;
propValues = inputStruct.propValues;
groupIndices = inputStruct.groupIndices;
groupNames = inputStruct.groupNames;
chanIndices = inputStruct.chanIndices;
chanNames = inputStruct.chanNames;
rootIndex = inputStruct.rootIndex;
data = inputStruct.data;
rootPropStruct = propsToStruct(propNames{rootIndex},propValues{rootIndex},REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND);
output = struct('props',rootPropStruct);
for iGroup = 1:length(groupIndices)
curGroupIndex = groupIndices(iGroup);
curChanIndices = chanIndices{iGroup};
curChanNames = chanNames{iGroup};
groupPropStruct = propsToStruct(propNames{curGroupIndex},propValues{curGroupIndex},REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND);
groupStruct = struct('name',groupNames(iGroup),'props',groupPropStruct);
for iChan = 1:length(curChanIndices)
curChanIndex = curChanIndices(iChan);
chanPropStruct = propsToStruct(propNames{curChanIndex},propValues{curChanIndex},REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND);
chanStruct = struct('name',curChanNames{iChan},'props',chanPropStruct,...
'data',[]);
chanStruct.data = data{curChanIndex};
%NOTE: I had a case statement in case the data type was a string,
%which would change the interpretation of the struct input
groupStruct.(TDMS_genvarname2(chanStruct.name,...
REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND)) = chanStruct;
end
output.(TDMS_genvarname2(groupStruct.name,...
REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND)) = groupStruct;
end
end
function propStruct = propsToStruct(names,values,REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND)
propStruct = struct([]);
for iProp = 1:length(names)
propStruct(1).(TDMS_genvarname2(names{iProp},REPLACE_STR,PREPEND_STR,ALWAYS_PREPEND)) = values{iProp};
end
end