-
Notifications
You must be signed in to change notification settings - Fork 1
/
Import_Gamry_To_Mat.asv
601 lines (500 loc) · 19.6 KB
/
Import_Gamry_To_Mat.asv
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
function Import_Gamry_To_Mat(varargin)
% Syntax of use
% Import_Maccor_To_Mat()
% Import_Maccor_To_Mat(pwd)
% Import_Maccor_To_Mat(pwd,pwd)
% Import_Maccor_To_Mat(pwd,pwd,[headerRowPosition,dataRowPosition])
% Import_Maccor_To_Mat(pwd,pwd,[headerRowPosition,dataRowPosition],'saveNames',{matFileName1,...,matFileNameN})
% Import_Maccor_To_Mat('currSgn',1)
% Import_Maccor_To_Mat('saveNames',{matFileName1,...,matFileNameN})
%
%
% Description of function
% Import data from Gamry DTA file(s) and save as MAT file(s).
% If there is no input, or the first input is an empty array, a file
% browser will open so that files can be selected.
%
% Input 1 (optional) Can be any of the following:
% An empty array (see above)
% A single file name as a string;
% A cell array of file names;
% A single folder name as a string.
% Default is []
%
% Input 2 (optional): a string for a folder to save the file(s) in. If an
% empty array is passed in, a GUI will open to select a folder. Default
% is [].
%
% Input 3 (optional): a 2 element numeric vector containing the row number
% which has the column names, and the row at which the main data begins.
% Common examples include [14,16], [1,3] and [0,1]. If the first element is
% set to 0 then no column names are assumed. Default is [].
%
% Input 4 (optional): A property-value input value of 1 or -1.
% Property: currSgn
% Value: Depending on how Maccor export settings are set, this property should be set to
% 1, if dicharge current is assumed positive when exporting the DTA file or to -1 if
% exported assuming negative for discharge. Default is 1.
%
% Input 5 (optional): A property-value input specifying file names to use
% when saving the converted DTA file.
% Property: saveNames
% Values: A single file name as a string or cell array of file names
% If no filenames are specified in Input 1, saveNames will be used to save results alphabetically
% If filenames are specified, saveNames will correspond to order of
% filenames in Input 1. Default is [].
%
% The file name to convert can contain no path or a partial path. File names don't
% have to have the .DTA extension. If no save path is passed to the
% function, a file browser will open to let the user select a folder.
% Currently the import function can only handle the 15 row header
% spreadsheet option from the Bitrode. Don't edit the DTA files in Excel
% because it saves it as a different type of (compressed) DTA and the
% header changes so the code errors out.
%
%
% Original script by: Thomas Bruen 07-May-2015 11:16:25
% Edited to handle input arguments and saving names by: W.D. Widanage 09-July-2015
% Edited to handle DTA files with problamatic headers and time columns by W. D. Widanage 21-Dec-2015
% Edited to handel Gamry txt file W. D. Widanage 23-Mar-2019
% Edited to handel Gamry meta info W. D. Widanage 26-Oct-2019
parObj = inputParser; % Create an input parse object to handle positional and property-value arguments
% Create variable names and assign default values after checking the value
% validity - 09-July-2015
addOptional(parObj,'dtaTxtDetails',[], @checkDTATxtDetails);
addOptional(parObj,'savePath',[], @checkSavePath);
addOptional(parObj,'hdPos',[], @checkHdPos);
addParameter(parObj,'currSgn',1, @isnumeric);
addParameter(parObj,'saveNames',[], @checkSaveNames);
addParameter(parObj,'fileType','.DTA'); % Assume default Maccor file type is DTA
addParameter(parObj,'metaPos',[], @checkMetaPos);
% Re-parse parObj - 09-July-2015
parse(parObj,varargin{:});
if isempty(parObj.Results.dtaTxtDetails)
%open GUI to find files
[fileNames,filePath,~] = uigetfile('*.DTA','Select DTA File(s)','MultiSelect','On');
if isnumeric(fileNames) %when cancelled, UIGETFILE returns 0
warning('No files selected')
return
elseif ischar(fileNames)
fullFileNames = {[filePath,fileNames]};
else
fullFileNames = cellfun(@(f) sprintf('%s%s',filePath,f),fileNames,'UniformOutput',false)';
end
else
% Could be a single file, or a cell array of files or directory. Pass to
% function to process and return a cell array of file names with path
fullFileNames = Input_Process(parObj);
end
%check all files are DTAs and exist.
% fullFileNames = Check_Filenames(fullFileNames,parObj);
%specify a directory to save MAT files in:
if ~isempty(parObj.Results.savePath)
savePath = parObj.Results.savePath;
if ~ischar(savePath) || ~isdir(savePath)
error('Save path cannot be found')
end
else
%no location requested: open file browser to select a folder
savePath = uigetdir;
if isnumeric(savePath) %user cancelled
warning('No save path selected')
return
end
end
%loop through files and send to function to get data, then save
nFiles = numel(fullFileNames);
for n = 1:nFiles
currentFile = fullFileNames{n};
metaInfo = metaDataStr(currentFile,parObj);
hdrData = HdrDataRow(currentFile,parObj); % Call function determine header and data row
tic
data = Import_Data(currentFile,hdrData(1),hdrData(2),parObj);
data.metaInfo = metaInfo;
toc
if isempty(parObj.Results.saveNames)
[~,f,~] = fileparts(currentFile);
else
if ischar(parObj.Results.saveNames)
[numSaveFiles,~] = size(parObj.Results.saveNames);
if numSaveFiles ~= nFiles
error('Number of DTA files is not equal to number of save file names');
end
f = parObj.Results.saveNames;
end
if iscell(parObj.Results.saveNames)
numSaveFiles = numel(parObj.Results.saveNames);
if numSaveFiles ~= nFiles
error('Number of DTA files is not equal to number of save file names');
end
f = parObj.Results.saveNames{n};
end
end
saveFile = [savePath,filesep,f,'.mat'];
save(saveFile,'-struct','data')
end
%%%%%% End of main function %%%%%%%
% Check validty of inputs - 09-July-2015
function valid = checkDTATxtDetails(dtaTxtDetails)
% Check if dtaTxtDetails is a string or cell array of strings
if strcmp(dtaTxtDetails,'saveNames')
valid = false;
elseif ischar(dtaTxtDetails)
valid = true;
elseif all(cellfun(@ischar,dtaTxtDetails))
valid = true;
else
error('Argument must be a string, or cell array of strings');
valid = false;
end
% Check validty of inputs - 09-July-2015
function valid = checkSavePath(savePath)
% Check if savePath is a valid string and directory
if strcmp(savePath,'saveNames')
valid = false;
elseif ischar(savePath) || isdir(savePath)
valid = true;
else
error('Path cannot be found')
valid = false;
end
% Check validty of inputs - 09-July-2015
function valid = checkHdPos(hdPos)
% Check if hdPos is a valid numeric vector
if strcmp(hdPos,'saveNames')
valid = false;
elseif isnumeric(hdPos) && numel(hdPos) == 2
valid = true;
else
error('Invalid values for header and data position, must be a numeric vector with two elements');
valid = false;
end
% Check validty of inputs - 09-July-2015
function valid = checkSaveNames(saveNames)
% Check if saveNames are strings
if ischar(saveNames)
valid = true;
elseif all(cellfun(@ischar,saveNames))
valid = true;
else
error('Invalid save names')
valid = false;
end
% Check validty of inputs - 09-July-2015
function valid = checkMetaPos(metaPos)
% Check if metaPos is a valid numeric vector
if strcmp(metaPos,'metaPos')
valid = false;
elseif isnumeric(metaPos) && numel(metaPos) == 2
valid = true;
else
error('Invalid values for meta data position, must be a numeric vector with two elements');
valid = false;
end
function fullFileNames = Input_Process(arg)
% parse first input to main function.
if ischar(arg.Results.dtaTxtDetails)
argCell = {arg.Results.dtaTxtDetails}; %cell so it can be treated the same as if a cell array
elseif iscell(arg.Results.dtaTxtDetails)
argCell = arg.Results.dtaTxtDetails;
else
error('Argument must be a string, or cell array of strings')
end
%get file(s) name with path for three cases: a cell array of file names, a
%single file name or a directory (in which case find the files in the
%directory).
if numel(argCell) > 1 %cell array: should be filenames
fullFileNames = argCell;
else
%determine if input is a directory or a single file
if isdir(argCell{1})
%is a directory: find all dta files
filePath = argCell{1};
if strcmp(arg.Results.fileType,'.DTA')
D = dir(sprintf('%s%s%s',filePath,filesep,'*.DTA'));
fileNames = {D.name}';
if isempty(fileNames)
error('No Maccor dta files found in directory')
else
fullFileNames = cellfun(@(f) sprintf('%s%s%s',filePath,filesep,f),fileNames,'UniformOutput',false);
end
elseif strcmp(arg.Results.fileType,'.txt')
D = dir(sprintf('%s%s%s',filePath,filesep,'*.txt'));
fileNames = {D.name}';
if isempty(fileNames)
error('No Maccor txt files found in directory')
else
fullFileNames = cellfun(@(f) sprintf('%s%s%s',filePath,filesep,f),fileNames,'UniformOutput',false);
end
end
else
%assume it is a file with path for now (check later)
fullFileNames = argCell(1); %keep as cell array
end
end
function fullFileNames = Check_Filenames(fullFileNames, parObj)
%check that the file names are dtas and all exist.
for n = 1:numel(fullFileNames)
[p,f,e] = fileparts(fullFileNames{n});
%add dta extension if there is none
if isempty(e) & strcmp(parObj.Results.fileType,'.DTA')
e = '.DTA';
elseif isempty(e) & strcmp(parObj.Results.fileType,'.txt')
e = '.txt';
end
%check extension is correct
if ~strcmp(lower(e),'.DTA') & strcmp(parObj.Results.fileType,'.DTA')
error('%s is not a DTA file.',fullFileNames{n});
end
if ~strcmp(lower(e),'.txt') & strcmp(parObj.Results.fileType,'.txt')
error('%s is not a txt file.',fullFileNames{n});
end
%if there is no path, assume current directory
if isempty(p)
p = pwd; %current folder
end
%rebuild filename
fullFileNames{n} = [p,filesep,f,e];
end
%check files all exist:
fileChk = cellfun(@(f) exist(f,'file') == 2,fullFileNames);
nChk = numel(fileChk(fileChk == 0));
missingFiles = fullFileNames(~fileChk);
if nChk
errorFormatString = repmat('%s\n',1,nChk);
error(sprintf('The following files cannot be found: \n%s',errorFormatString),missingFiles{:});
end
function hdrDataVec = HdrDataRow(currentFile,parObj)
% Automatically determine the start of the header and data row. Three
% possible otptions.
% 1. If first row has the word 'Test', the DTA file has the extra file
% info. Therefore header and data rows are 14 and 16
% 2. If first row has the word 'Current', the bitrode dta file starts with the
% header. Therefore header and data rows are 1 and 3
% 3. Otherwise assume first row is the data row. Therefore header and data
% rows are 0 and 3
%open file
fid = fopen(currentFile);
frewind(fid); %just in case
fstRowStr = fgetl(fid);
if numel(parObj.Results.hdPos) == 2
hdrDataVec= parObj.Results.hdPos;
elseif regexp(fstRowStr,'Test')
hdrDataVec = [14,16];
elseif regexp(fstRowStr,'Current')
hdrDataVec = [1,3];
else
hdrDataVec = [0,3];
end
function dataStruct = Import_Data(fileName,rowColumnNames,rowDataStart,parObj)
%main routine to import data. File is loaded using FREAD and the header is
%processed to get the column names (using a subfunction). These column
%names are used as structure field names for the data. TEXTSCAN is used to
%read in the data.
%open file
fid = fopen(fileName);
frewind(fid); %just in case
%get row of column names, if there are any. If not, handle later
if rowColumnNames > 0
%skip over headers:
for n = 1:rowColumnNames-1 %-1: fgetl goes to the next line
[~] = fgetl(fid);
end
columnNameText = fgetl(fid); % channel names
else
columnNameText = [];
end
%skip to data
for n = 1:(rowDataStart - rowColumnNames -1)
[~] = fgetl(fid);
end
%get first row of data to test the format, then go back to start of data
dataStartPosition = ftell(fid);
dataFirstLine = fgetl(fid);
fseek(fid,dataStartPosition,'bof');
%get preallocated structure and channel types (string or numeric)
[dataStruct,formatString] = Process_Channel_Names(columnNameText,dataFirstLine,parObj);
nChannels = numel(dataStruct.channelNames);
dataStruct.dtaInfo = dir(fileName);
if strcmp(parObj.Results.fileType,'.DTA')
data = textscan(fid,formatString,'delimiter',' ');
elseif strcmp(parObj.Results.fileType,'.txt')
data = textscan(fid,formatString,'delimiter','\t');
end
fclose(fid);
% save columns of data to structure, depending on if string or numeric
dataNames = fieldnames(dataStruct.data);
for n = 1:nChannels
dataStruct.data.(dataNames{n}) = data{n};
if strcmp(dataNames{n},'State')
stateIdx = n;
end
if strcmp(dataNames{n},'Md')
stateIdx = n;
end
end
% Make discharge current -ve
if( parObj.Results.currSgn == -1)
if isfield(dataStruct.data,'Amps')
currentTmp = dataStruct.data.Amps;
isDischarge = cellfun(@strcmp,data{stateIdx},repmat({'D'},length(data{stateIdx}),1));
dataStruct.data.Amps(isDischarge) = currentTmp(isDischarge)*parObj.Results.currSgn; %
dataStruct.nSamples = size(dataStruct.data.(dataNames{n}),1);
end
if isfield(dataStruct.data,'Current_A_')
currentTmp = dataStruct.data.Current_A_;
isDischarge = cellfun(@strcmp,data{stateIdx},repmat({'D'},length(data{stateIdx}),1));
dataStruct.data.Current_A_(isDischarge) = currentTmp(isDischarge)*parObj.Results.currSgn; %
dataStruct.nSamples = size(dataStruct.data.(dataNames{n}),1);
end
end
if isfield(dataStruct.data,'TestTime')
if iscell(dataStruct.data.TestTime(1))
dataStruct.data.TestTime = Time_String_To_Seconds(dataStruct.data.TestTime);
end
end
function [dataStruct,formatString] = Process_Channel_Names(channelString,dataString,parObj)
%get channel names, and test to determine which channels are text and whcih
%are numeric.
%channelString (char array): row of spreadsheet containing channel names.
%dataString (char array): row of spreadsheet containing a row of data
% %use first line of data to see how many channels there are:
% nChannels = numel(strfind(dataString,','))+1;
%read all data channels as a string, then see which can be converted to
%numbers:
dataTextTmp = split(dataString);
empData = cellfun(@isempty,dataTextTmp);
dataText = dataTextTmp(~empData);
nChannels = numel(dataText)-1;
dataConv = cellfun(@str2double,dataText);
formatString = repmat('%f',1,nChannels);
formatString(find(isnan(dataConv))*2) = 's';
%if there aren't any column names row, then create a dummy one
if isempty(channelString) % No headers are specified. Use default, Channel_1...Channel_N
channelNamesTmp = sprintf('Channel_%d',1:nChannels);
channelNames = regexp(channelNamesTmp,'Channel_[\d]{0,}','match'); % Get channel hdr names
units = repmat({'None'},nChannels,1);
else
expHdr = { 'Pt', 'T', 'Vf', 'Im', 'Vu', 'Sig', 'Ach', 'IERange', 'Over', 'Temp'};
channelNamesTmp = split(channelString);
channelNames = channelNamesTmp(2:nChannels)';
units = repmat({'None'},nChannels,1);
end
%create a structure to store the data in:
structNames = matlab.lang.makeValidName(channelNames); %make channel names valid field names
uniChnlNames = UniqueChannelNames(structNames); % Handle repeating channel names
structDummy = [uniChnlNames;cell(1,nChannels)];
dataStruct.data = struct(structDummy{:});
%add channel info to structure
dataStruct.channelNames = channelNames;
dataStruct.channelUnits = units;
function chnlHdrs = UniqueChannelNames(chnlHdrs)
nEl = length(chnlHdrs);
[~,uniIdx] = unique(chnlHdrs);
repIdx = setdiff([1:nEl],uniIdx);
for ii = 1:length(repIdx)
chnlHdrs(repIdx(ii)) = {[chnlHdrs{repIdx(ii)},sprintf('_%d',ii)]};
end
function timeVec = Time_String_To_Seconds(timeCell)
N = length(timeCell);
% numDays = str2num(cellfun(@extractBefore,timeCell,repmat({'d'},N,1)));
numDaysTmp = cellfun(@extractBefore,timeCell,repmat({'d'},N,1),'UniformOutput',false);
numDays = cellfun(@str2num,numDaysTmp);
hmsCell = cellfun(@extractAfter,timeCell,repmat({'d '},N,1),'UniformOutput',false);
durCell = cellfun(@duration,hmsCell);
timeVecSec = seconds(durCell);
% if idxZ(1) == 1
% secR = cumsum(secTmp(idxZ(2:end)-1));
% timeVecSec(1:idxZ(2)-1,1) = secTmp(1:idxZ(2)-1);
% for jj = 1:length(secR)-1
% idxTmp = idxZ(jj+1):idxZ(jj+2)-1;
% timeVecSec(idxTmp,1) = secTmp(idxTmp) + secR(jj);
% end
% else
% secR = cumsum(secTmp(idxZ-1));
% timeVecSec(1:idxZ(1)-1,1) = secTmp(1:idxZ(1)-1);
% for jj = 1:length(secR)-1
% idxTmp = idxZ(jj):idxZ(jj+1)-1;
% timeVecSec(idxTmp,1) = secTmp(idxTmp) + secR(jj);
% end
% end
timeVec = timeVecSec - timeVecSec(1) + numDays*86400;
function metaInfo = metaDataStr(currentFile,parObj)
% Extract the meta data infomation
fid = fopen(currentFile);
frewind(fid);
if numel(parObj.Results.metaPos) == 2
metaDataSrt = parObj.Results.metaPos(1);
metaDataEnd = parObj.Results.metaPos(2);
%skip to meta data
for n = 1:(metaDataSrt-1)
[~] = fgetl(fid);
end
for n = 1 : metaDataEnd - metaDataSrt +1
metaInfo{n,1} = fgetl(fid);
end
else
metaInfo = []; % Set meta info as null
end
% Redundant functions
% function timeVec = Time_String_To_Seconds(timeCell)
% %convert a cell array of time strings to the time in seconds
% N = numel(timeCell);
% timeVec = zeros(N,1);
%
% tempTimeStr = regexp(timeCell{1},'\"', 'split');
% if length(tempTimeStr) == 1 % The time string does not contain "
% timeCmptns = regexp(tempTimeStr{1},'\:', 'split');
% if length(timeCmptns) == 2 % time is in M:S.S format
% timeFormat = '%d:%d.%d';
% elseif length(timeCmptns) == 3 % time is in H:M:S.S format
% timeFormat = '%d:%d:%d.%d';
% end
% else % The time string does contain "
% timeCmptns = regexp(tempTimeStr{2},'\:', 'split');
% if length(timeCmptns) == 2 % time is in M:S.S format
% timeFormat = '="%d:%d.%d"';
% elseif length(timeCmptns) == 3 % time is in H:M:S.S format
% timeFormat = '="%d:%d:%d.%d"';
% end
% end
%
% for n = 1:N
% timeElements = sscanf(timeCell{n},timeFormat);
% if isempty(timeElements)
% continue
% end
% % Last element of timeElements is the value after the decimal point.
% % Convert back to decimal from units. The logic is if the last element
% % is for example 55 this should be converted to 0.55. To do represent
% % the number as 0.55E-2 and get the value of the exponent. Then divide
% % the last element by 10^2.
% expo = ceil(log10([timeElements(end)+ 0.1])); % The 0.1 is added to ensure that all 10^n values return n+1 as the exponent when rounding up
% div = 10^expo;
% if length(timeElements) == 3
% multiplier = [60, 1, 1/div];
% elseif length(timeElements) == 4
% multiplier = [3600, 60, 1, 1/div];
% end
% if ~isempty(timeElements)
% timeVec(n) = multiplier*timeElements;
% end
% end
%
% idx = find(diff(timeVec)<0);
%
% if ~isempty(idx)
% for ii = 1:length(idx)+1
% if ii == 1
% sIdx = 1;
% eIdx = idx(ii);
% elseif ii == length(idx)+1
% sIdx = idx(ii-1)+1;
% eIdx = N;
% else
% sIdx = idx(ii-1)+1;
% eIdx = idx(ii);
% end
% timeVec(sIdx:eIdx) = timeVec(sIdx:eIdx)+3600*(ii-1);
% end
% end