forked from humphreysb/ConvertTDMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleConvertTDMS.m
139 lines (117 loc) · 5.18 KB
/
simpleConvertTDMS.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function matFileName=simpleConvertTDMS(fileName,ignoreGroupNames)
%matFileName=simpleConvertTDMS(fileName)
%
%Function to convert .tdms files to .mat files. This function calls
%convertTDMS and creates a .mat file for each .tdms file provided. It
%a more intuitive set of variables to the .mat file (which can then be
%loaded into the Matlab workspace with load command).
%
% Inputs:
% filename (optional) - Filename to be converted.
% If not supplied, the user is provided a 'File Open' dialog box
% to navigate to a file. Can be a cell array of files for bulk
% conversion.
% ignoreGroupNames (optional) - if 1, use only the channel
% names in creating the data variables. (See below)
%
%
% Outputs:
%
% matFileName - Structure array containing the names of the
% new mat files.
%
% Example:
% To convert/load a single file:
% matFileName=simpleConvertTDMS('nameOfTDMS.tdms');
% load(matFileName);
%
% To get a list of the channels in matFileName, use whos:
% whos -file matFileName
%
% To get one channel from the .mat file, use whos and then load only
% the channel:
% load(matFileName,'nameOfChannelFromWhosOutput')
%
% Comments:
% ignoreGroupNames: This was added
% because LV uses a ugly (timestamp) for the default
% group name if groupname is not supplied by the LV programmer.
% Caution should be excersised when setting to 1 as multiple
% groups can be in a tdms file with the same channel names and
% this would cause cahnnels to be overwritten during conversion.
%
% See also: convertTDMS
%-------------------------------------------------------------------------
%Brad Humphreys - v1.0 2013-11-16
%ZIN Technologies
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%Brad Humphreys - v1.1 2014-1-13
%Added check to see if the group/channel name begins with an alpha
%char. LV allows group and channel names to begin with numeric char.
%If first char are numeric, a "d" is prepended to the group name
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%Brad Humphreys - v1.2 2014-2-7
%Added example function call to help.
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%Brad Humphreys - v1.3 2014-5-27
%Added ignoreGroupNames option and replace strsplit function to make more
%backwards compatible per suggestion from Randy82.
%-------------------------------------------------------------------------
if nargin==0
%Prompt the user for the file
[fileName,pathName]=uigetfile({'*.tdms','All Files (*.tdms)'},'Choose a TDMS File');
if fileName==0
return
end
fileName=fullfile(pathName,fileName);
end
if nargin<2 %Default to not ignoring groupnames
ignoreGroupNames=0;
end
if iscell(fileName)
%For a list of files
inFileName=fileName;
else
inFileName=cellstr(fileName);
end
for fnum=1:numel(inFileName) % Loop through cells of filename
tdmsFileName=inFileName{fnum};
%Perform Conversion
[convertedData,dataOb.convertVer,d.chanNames,d.groupNames,dataOb.ci]=convertTDMS(0,tdmsFileName);
%Build more obvious struture of data form file
dataOb.fileName=convertedData.FileName;
dataOb.fileFolder=convertedData.FileFolder;
channelNames={convertedData.Data.MeasuredData.Name};
for cnum=1:numel(channelNames)
if ignoreGroupNames %Drop the group name
if ~isempty(strfind(channelNames{cnum},'/'));
%splitName=strsplit(channelNames{cnum},'/');
splitName=regexp(channelNames{cnum}, '/', 'split');
channelNames{cnum}=splitName{end};
end
end
safeChannelName{cnum} = regexprep(channelNames{cnum},'\W',''); %Remove charters not compatible for variable names
chanNameTemp=safeChannelName{cnum};
if ~isstrprop(chanNameTemp(1),'alpha'); %If the first character is not alpha, append a 'd'
safeChannelName{cnum}=['d' safeChannelName{cnum}];
end
dataOb.(safeChannelName{cnum}).Data=convertedData.Data.MeasuredData(cnum).Data;
dataOb.(safeChannelName{cnum}).Total_Samples=convertedData.Data.MeasuredData(cnum).Total_Samples;
%Convert the properties
prop=convertedData.Data.MeasuredData(cnum).Property;
for pcnt=1:numel(prop)
pName=prop(pcnt).Name;
safePropName= regexprep(pName,'\W','');
pValue=prop(pcnt).Value;
dataOb.(safeChannelName{cnum}).Property.(safePropName)=pValue;
end
end
%Write the file
[pathstr,name,ext]=fileparts(tdmsFileName);
matFileName{fnum}=fullfile(pathstr,[name '.mat']);
save(matFileName{fnum},'-struct','dataOb');
fprintf('Saved File ''%s''\n',matFileName{fnum})
end