-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataAcquisition.m
1416 lines (1251 loc) · 59.5 KB
/
DataAcquisition.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef DataAcquisition < handle
% DATAACQUISITION is a Matlab program for electronic signal acquistion from
% a National Instruments DAQ USB-6003.
%
% A graphical user interface can be launched from the command line in
% Matlab by typing
% >> DataAcquisition
% at the command prompt.
%
% This software was written with Patch Clamp and electrophysiological
% measurements in mind, although it could also be used as a simple
% oscilloscope and data recording tool whenever measurements are made using
% a DAQ from National Instruments.
%
% DataAcquisition has the following modes of operation, which can be
% accessed via the Mode menu.
%
% "Normal Acquisition":
% An oscilloscope mode, enabling live data viewing as well as "gap-free"
% recording.
% "Noise Plot":
% A live noise plot is displayed on the screen and is periodically
% refreshed, allowing the user to identify noise sources, make changes in
% an experiment, and see their effects in real time.
% "IV Curve":
% Programmable, periodic voltage stimulation allows the user to make a
% recording of current versus voltage. This feature displays the results
% on screen in a second panel.
% "Membrane Seal Test":
% A mode used for examining capacitance. Reapeatedly
% applies a voltage step function and displays the resulting current
% response. This mode is useful in patch clamp experiments, where the
% user can adjust capacitance compensation to offset the current spikes
% which accompany voltage steps that are applied across a membrane.
%
% The configuration of the DAQ, including scale factors specific to the
% current amplifier being used, can be set by navigating to
% File > Configure DAQ.
%
% Use of the functionality specific to electrophysiology measurements (IV
% curve and membrane seal test) require the analog output terminal 'ao0' of
% the DAQ to be connected to an external command input on the current
% amplifier.
%
% Data is saved in a .dbf file format in the folder specified by the user
% (can be set by navigating to File > Choose Save Directory).
% The .dbf file format, for "Dataacquisition Binary Format," is a binary
% file which contains a header followed by the signal data, and can be
% opened using Tamas Szalay's PoreView software, with the appropriate
% additions for .dbf handling.
%
% Data in .dbf files can also be converted to other filetypes for use in
% other data analysis pipelines. Data can be converted via
% File > Convert Data File
% using the dialog that pops up.
%
% Stephen Fleming 2016.08.13
% latest updates 2018.04.25
properties
fig % Handle for the entire DataAcquisition figure
DAQ % Matlab DAQ object handle
file % Information about the next data file to be saved
mode % Which program is running: normal, noise, iv, sealtest
alpha % Conversion factors from input to real signal
outputAlpha % Conversion factor for output
channels % Channels for data acquisition
sampling % Sampling frequency for input channels
end
properties (Hidden=true)
panel % Handle for main display panel
axes % Handle for the main axes
fig_cache % Handle for cache object that handles live data
DEFS % UI definitions (widths etc.)
tabs % Main window tabs handle struct
hcmenu % Handle to context menu
tbuts % Handles for the main control buttons
ybuts % Handles for the y axis buttons
xbuts % Handles for the x axis buttons
outputFreq % Frequency of analog output signal
audio % Matlab audio player object
data_compression_scaling % compression to int16
end
methods (Hidden=true)
function obj = DataAcquisition(varargin)
% Constructor
% Handle inputs
inputs = obj.parseInputs(varargin);
obj.channels = inputs.Channels;
obj.alpha = inputs.Alphas;
obj.sampling = inputs.SampleFrequency;
obj.outputAlpha = inputs.OutputAlpha;
% Figure out scale factor for data compression
% (i.e. lossless representation as a 16-bit integer)
obj.data_compression_scaling = (2^15-1)./obj.alpha/10; % range of 10V
% Set constants
obj.outputFreq = 100;
% Initialize DAQ
function startDAQ(~,~)
try
obj.DAQ.s = daq.createSession('ni');
addAnalogInputChannel(obj.DAQ.s, 'Dev1', obj.channels, 'Voltage');
obj.DAQ.s.Rate = obj.sampling;
obj.DAQ.ao = daq.createSession('ni');
addAnalogOutputChannel(obj.DAQ.ao, 'Dev1', 'ao0', 'Voltage');
obj.DAQ.ao.Rate = obj.outputFreq;
obj.DAQ.s.IsContinuous = true;
obj.DAQ.ao.IsContinuous = false;
display('DAQ communication established...')
catch ex
display('Problem initializing DAQ!')
end
try
% pre-run to warm up and prevent lag later
obj.DAQ.listeners.plot = addlistener(obj.DAQ.s, 'DataAvailable', ...
@(~,~) 0);
obj.DAQ.s.startBackground;
obj.DAQ.s.stop;
display('DAQ successfully initialized.')
catch ex
end
end
startDAQ;
% Initialize temporary file and file data
function setFileLocation(~,~)
c = clock;
% get the folder location from the user
obj.file.folder = [uigetdir(['C:\Data\PatchClamp\' num2str(c(1)) ...
sprintf('%02d',c(2)) sprintf('%02d',c(3))], ...
'Select save location') '\'];
% if unable to get input from user
if any([isempty(obj.file.folder), obj.file.folder == 0])
obj.file.folder = ['C:\Data\PatchClamp\' num2str(c(1)) sprintf('%02d',c(2)) sprintf('%02d',c(3)) '\'];
end
obj.file.prefix = [num2str(c(1)) '_' sprintf('%02d',c(2)) '_' sprintf('%02d',c(3))];
obj.file.suffix = '.dbf';
obj.file.num = 0;
obj.file.name = [obj.file.folder obj.file.prefix '_' sprintf('%04d',obj.file.num) obj.file.suffix];
obj.file.fid = [];
end
setFileLocation;
% Create space for listeners
obj.DAQ.listeners = [];
% Enable the user to change DAQ configuation settings
function configure(~,~)
% open up a dialog box where users can change the DAQ
% configuration
prompt = {'Channels (array of integers):', ...
'Scaling of inputs (array: measured*scaling = pA or mV):', ...
'Scaling of output (number: output(mV)*scaling = Volts in DAQ''s output range)', ...
'Sampling frequency (number in Hz):'};
dlg_title = 'DAQ configuration';
defaultans = {['[' num2str(obj.channels) ']'], ...
['[' num2str(obj.alpha) ']'], ...
num2str(obj.outputAlpha), ...
num2str(obj.sampling)};
answer = inputdlg(prompt,dlg_title,1,defaultans);
if ~isempty(answer) % user did not press 'cancel'
% construct the argument list of name value pairs
emptyinputs = cellfun(@(x) isempty(x), answer);
names = {'Channels','Alphas','OutputAlpha','SampleFrequency'};
values = cellfun(@(x) str2num(x), answer, 'uniformoutput', false);
listargs = reshape([names(~emptyinputs); values(~emptyinputs)'],1,[]);
% re-configure the DAQ
inputs = obj.parseInputs(listargs);
obj.channels = inputs.Channels;
obj.alpha = inputs.Alphas;
obj.sampling = inputs.SampleFrequency;
obj.outputAlpha = inputs.OutputAlpha;
% update parameters in DAQ config and in figure cache
startDAQ;
obj.mode = 'normal';
obj.normalMode;
end
end
% Enable the user to convert DBF file to another filetype
function convertDataFile(~,~)
% convert file from the DBF filetype to another filetype,
% either HDF or CSV, which will be larger files, but can be
% used in any data analysis pipeline.
% open a dialog to prompt user to select a file for
% conversion
[dataFile, dataPath] = uigetfile([obj.file.folder '/*.dbf'], ...
'Select DBF file to convert');
% if unable to get input file from user
if any([isempty(dataFile), dataFile == 0])
display('Conversion aborted.')
return;
end
% pop up a dialog to let user choose desired file type
filetype = questdlg('What is the desired output file type?', ...
'File converter', 'hdf', 'csv', 'Cancel', 'Cancel');
% if user chose to cancel
if strcmp(filetype,'Cancel')
display('Conversion aborted.')
return;
end
% set up new file, same name .csv or .hdf
newFileName = [dataPath, dataFile(1:end-3), filetype];
import matlab.io.hdf4.* % scope this import for the whole
try
% open data file
oldFileName = [dataPath, dataFile];
[~, h] = dbfload(oldFileName, 'info');
chunk = 2e5; % 200k data points at a time
% if CSV, display metadata at command window
if strcmp(filetype,'csv')
display('Important file metadata will not be saved with CSV.')
display('File metadata:')
display(['Sampling interval = ' num2str(h.si) ' seconds'])
display('Channels recorded:')
display(h.chNames)
display(['Number of data points recorded (for each channel) = ' num2str(h.numPts)])
% put in initial headers in csv file
fid = fopen(newFileName, 'W'); % capital W
fmt = [repmat('%s,', 1, length(h.chNames)) '\r\n'];
fprintf(fid, fmt, h.chNames{:});
fmtstr = [repmat('%.3f,', 1, length(h.chNames)) '\r\n'];
fmt = repmat(fmtstr, 1, chunk);
else
% it's an hdf file, which needs to be initialized
%delete newFileName % this seems to be necessary
sdID = sd.start(newFileName,'create');
% write meta-data from header into the hdf file
sd.setAttr(sdID,'sampling_interval',h.si);
sd.setAttr(sdID,'samples_per_channel',h.numPts);
sd.setAttr(sdID,'number_of_channels',h.numChan);
sd.setAttr(sdID,'convert_int16_to_double_by_dividing_by',h.data_compression_scaling);
for i = 0:h.numChan-1
sdsIDs(i+1) = sd.create(sdID,h.chNames{i+1},'int16',h.numPts);
% calibration to go from 16-bit int to actual value
sd.setCal(sdsIDs(i+1),1/h.data_compression_scaling(i+1),0,0,0,'int16'); % datatype of uncalibrated data
% info
sd.setDataStrs(sdsIDs(i+1),[h.chNames{i+1} ' = int16 value * scale_factor'],h.chNames{i+1}(end-2:end-1), ...
'int16',sprintf('Timepoints sampled at intervals of %g seconds', h.si));
end
end
% load data in chunks and save in new format
fprintf('Converting dbf file to %s ... %3.0f%% ',filetype,0)
for i = 0:chunk:h.numPts
% load chunk
range = [i, min(h.numPts, i+chunk)];
% load and write chunk
if strcmp(filetype,'csv')
[d, ~] = dbfload(oldFileName, range, 'double');
% dlmwrite(newFileName,d,'-append','precision',5,'newline','pc'); % slow
if size(d,1) < chunk
fmt = repmat(fmtstr, 1, size(d,1)); % last chunk is smaller
end
fprintf(fid, fmt, d);
elseif strcmp(filetype,'hdf')% it's hdf
[d, ~] = dbfload(oldFileName, range, 'int16');
for chan = 1:size(d,2)
sd.writeData(sdsIDs(chan), i, d(:,chan));
end
end
% track completion
fprintf('\b\b\b\b\b%3.0f%% ',100*min([1, (i+chunk)/h.numPts]))
end
fprintf('\n')
% close file
if strcmp(filetype,'hdf')
arrayfun(@(x) sd.endAccess(x), sdsIDs);
sd.close(sdID);
elseif strcmp(filetype,'csv')
fclose(fid);
end
display(['Successfully saved new file ' newFileName])
catch ex % something went wrong...
% give up files
if strcmp(filetype,'hdf')
try
arrayfun(@(x) sd.endAccess(x), sdsIDs);
sd.close(sdID);
catch ex
end
elseif strcmp(filetype,'csv')
try
fclose(fid);
catch ex
end
end
display('Something went wrong in the conversion... do you have write access?')
end
end
% Define what happens on close
function closeProg(~,~)
% close figure
delete(obj.fig)
% delete stuff
try
arrayfun(@(lh) delete(lh), obj.DAQ.listeners);
delete(obj.fig_cache);
catch ex
end
% end DAQ sessions
try
stop(obj.DAQ.s);
stop(obj.DAQ.ao);
release(obj.DAQ.s);
release(obj.DAQ.ao);
delete(obj.DAQ);
catch ex
end
% delete the DataAcquistion object
try
delete(obj);
catch ex
end
end
% Create figure ===============================================
% some UI defs
obj.DEFS = [];
obj.DEFS.BIGBUTTONSIZE = 35;
obj.DEFS.BUTTONSIZE = 20;
obj.DEFS.PADDING = 2;
obj.DEFS.LABELWIDTH = 65;
% start making GUI objects
obj.fig = figure('Name','DataAcquisition','MenuBar','none',...
'NumberTitle','off','DockControls','off','Visible','off', ...
'DeleteFcn',@closeProg);
% set its position
oldunits = get(obj.fig,'Units');
set(obj.fig,'Units','normalized');
set(obj.fig,'Position',[0.1,0.1,0.8,0.8]);
set(obj.fig,'Units',oldunits);
% make the menu bar
f = uimenu('Label','File');
uimenu(f,'Label','Choose Save Directory','Callback',@setFileLocation);
uimenu(f,'Label','Configure DAQ','Callback',@configure);
uimenu(f,'Label','Convert Data File','Callback',@convertDataFile);
uimenu(f,'Label','Quit','Callback',@closeProg);
mm = uimenu('Label','Mode');
uimenu(mm,'Label','Normal acquisition','Callback',@(~,~) obj.normalMode);
uimenu(mm,'Label','IV curve','Callback',@(~,~) obj.ivMode);
uimenu(mm,'Label','Noise plot','Callback',@(~,~) obj.noiseMode);
uimenu(mm,'Label','Membrane seal test','Callback',@(~,~) obj.sealtestMode);
hm = uimenu('Label','Help');
uimenu(hm,'Label','DataAcquisition','Callback',@(~,~) doc('DataAcquisition.m'));
uimenu(hm,'Label','About','Callback',@(~,~) msgbox({'DataAcquisition v1.0 - written by Stephen Fleming.' '' ...
'Created for Harvard''s 2016 Freshaman Seminar course 25o.' '' ...
'This program and its author are not affiliated with National Instruments, Matlab, or Molecular Devices.' ''},'About DataAcquisition'));
obj.mode = 'normal';
obj.normalMode;
% =============================================================
end
function normalMode(obj)
% stop other things that might have been running
obj.stopAll;
obj.mode = 'normal';
% set default positions
obj.normalSizing;
obj.normalResizeFcn;
% Initialize figure cache
obj.fig_cache = figure_cache(obj.axes, obj.alpha, 5);
% Show figure
obj.fig.Visible = 'on';
end
function normalSizing(obj)
delete(obj.panel);
obj.panel = uipanel('Parent',obj.fig,'Position',[0 0 1 1]);
delete(obj.axes);
obj.axes = axes('Parent',obj.panel,'Position',[0.05 0.05 0.95 0.90],...
'GridLineStyle','-','XColor', 0.15*[1 1 1],'YColor', 0.15*[1 1 1]);
set(obj.axes,'NextPlot','add','XLimMode','manual');
set(obj.axes,'XGrid','on','YGrid','on','Tag','Axes','Box','on');
obj.axes.XLim = [0 5];
obj.axes.YLim = [-1 1];
obj.axes.YLabel.String = 'Current (pA)';
obj.axes.YLabel.Color = 'k';
obj.axes.XLabel.String = 'Time (s)';
obj.axes.XLabel.Color = 'k';
% now make the buttons
% y-axis
obj.ybuts = [];
obj.ybuts(1) = uicontrol('Parent', obj.panel, 'String','<html>-</html>',...
'callback', @(~,~) obj.fig_cache.zoom_y('out'));
obj.ybuts(2) = uicontrol('Parent', obj.panel, 'String','<html>↓</html>',...
'callback', @(~,~) obj.fig_cache.scroll_y('down'));
obj.ybuts(3) = uicontrol('Parent', obj.panel, 'String','<html>R</html>',...
'callback', @(~,~) obj.fig_cache.reset_fig);
obj.ybuts(4) = uicontrol('Parent', obj.panel, 'String','<html>↑</html>',...
'callback', @(~,~) obj.fig_cache.scroll_y('up'));
obj.ybuts(5) = uicontrol('Parent', obj.panel, 'String','<html>+</html>',...
'callback', @(~,~) obj.fig_cache.zoom_y('in'));
% x-axis
obj.xbuts = [];
obj.xbuts(1) = uicontrol('Parent', obj.panel, 'String','<html>-</html>',...
'callback', @(~,~) obj.fig_cache.zoom_x('out'));
obj.xbuts(2) = uicontrol('Parent', obj.panel, 'String','<html>+</html>',...
'callback', @(~,~) obj.fig_cache.zoom_x('in'));
% top
obj.tbuts = [];
obj.tbuts(1) = uicontrol('Parent', obj.panel, ...
'Style', 'togglebutton', 'CData', imread('Record.png'),...
'callback', @(src,~) obj.stateDecision(src), 'tag', 'record');
obj.tbuts(2) = uicontrol('Parent', obj.panel, ...
'Style', 'togglebutton', 'CData', imread('Play.png'),...
'callback', @(src,~) obj.stateDecision(src), 'tag', 'play');
% set the resize function
set(obj.panel, 'ResizeFcn', @(~,~) obj.normalResizeFcn);
% and call it to set default positions
obj.normalResizeFcn;
% Initialize figure cache
obj.fig_cache = figure_cache(obj.axes, obj.alpha, 5);
% Show figure
obj.fig.Visible = 'on';
end
function normalResizeFcn(obj)
% get size of panel in pixels
sz = obj.getPixelPos(obj.panel);
% position the axes object
sz(1) = sz(1) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % left
sz(3) = sz(3) - sz(1); % width
sz(2) = sz(2) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % bottom
sz(4) = sz(4) - sz(2) - obj.DEFS.BIGBUTTONSIZE - 3*obj.DEFS.PADDING; % height
set(obj.axes,'Position',max(1,sz),'Units','Pixels');
% get size of axes in pixels
sz = obj.getPixelPos(obj.axes);
% figure out where the y middle is
mid = sz(4)/2 + sz(2);
% position the buttons
for i=1:numel(obj.ybuts)
set(obj.ybuts(i),'Position',...
max(1,[obj.DEFS.PADDING, ...
mid+(i-numel(obj.ybuts)/2-1)*obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE]));
end
% figure out where the x middle is
mid = sz(3)/2 + sz(1);
% position the buttons
for i=1:numel(obj.xbuts)
set(obj.xbuts(i),'Position',...
max(1,[mid+(i-numel(obj.xbuts)/2-1)*obj.DEFS.BUTTONSIZE, ...
obj.DEFS.PADDING, ...
obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE]));
end
for i=1:numel(obj.tbuts)
set(obj.tbuts(i),'Position',...
max(1,[mid+(i-numel(obj.tbuts)/2-1)*obj.DEFS.BIGBUTTONSIZE, ...
sz(2) + sz(4) + obj.DEFS.PADDING, ...
obj.DEFS.BIGBUTTONSIZE, ...
obj.DEFS.BIGBUTTONSIZE]));
end
end
function noiseMode(obj)
% stop other things that might have been running
obj.stopAll;
obj.mode = 'noise';
% set default positions
obj.noiseSizing;
obj.noiseResizeFcn;
% No figure cache necessary
obj.fig_cache = [];
% Show figure
obj.fig.Visible = 'on';
end
function noiseSizing(obj)
delete(obj.panel);
obj.panel = uipanel('Parent',obj.fig,'Position',[0 0 1 1]);
delete(obj.axes);
obj.axes = axes('Parent',obj.panel,'Position',[0.05 0.05 0.95 0.90],...
'GridLineStyle','-','XColor', 0.15*[1 1 1],'YColor', 0.15*[1 1 1]);
set(obj.axes,'NextPlot','add','XLimMode','manual');
set(obj.axes,'XGrid','on','YGrid','on','Tag','Axes', ...
'Box','on','XScale','log','YScale','log');
obj.axes.YLabel.String = 'Current noise power spectral density (nA^2/Hz)';
obj.axes.YLabel.Color = 'k';
obj.axes.XLabel.String = 'Frequency (Hz)';
obj.axes.XLabel.Color = 'k';
obj.axes.YLim = [1e-14, 1e-7];
obj.axes.XLim = [1 3e4];
% now make the buttons
% y-axis
obj.ybuts = [];
obj.ybuts(1) = uicontrol('Parent', obj.panel, 'String','<html>-</html>',...
'callback', @(~,~) zoom_y('out'));
obj.ybuts(2) = uicontrol('Parent', obj.panel, 'String','<html>↓</html>',...
'callback', @(~,~) scroll_y('down'));
obj.ybuts(3) = uicontrol('Parent', obj.panel, 'String','<html>R</html>',...
'callback', @(~,~) reset_fig);
obj.ybuts(4) = uicontrol('Parent', obj.panel, 'String','<html>↑</html>',...
'callback', @(~,~) scroll_y('up'));
obj.ybuts(5) = uicontrol('Parent', obj.panel, 'String','<html>+</html>',...
'callback', @(~,~) zoom_y('in'));
function zoom_y(str)
if strcmp(str,'in')
obj.axes.YLim = 10.^(log10(get(obj.axes,'YLim')) + [1 -1]);
elseif strcmp(str,'out')
obj.axes.YLim = 10.^(log10(get(obj.axes,'YLim')) + [-1 1]);
end
end
function scroll_y(str)
if strcmp(str,'up')
obj.axes.YLim = 10.^(log10(get(obj.axes,'YLim')) + [1 1]);
elseif strcmp(str,'down')
obj.axes.YLim = 10.^(log10(get(obj.axes,'YLim')) + [-1 -1]);
end
end
function reset_fig
obj.axes.YLim = [1e-14, 1e-7];
obj.axes.XLim = [1 3e4];
end
% top
obj.tbuts = [];
obj.tbuts(1) = uicontrol('Parent', obj.panel, ...
'Style', 'togglebutton', 'CData', imread('Play.png'),...
'callback', @(src,~) obj.stateDecision(src), 'tag', 'noise');
% set the resize function
set(obj.panel, 'ResizeFcn', @(~,~) obj.noiseResizeFcn);
% and call it to set default positions
obj.noiseResizeFcn;
% Show figure
obj.fig.Visible = 'on';
end
function noiseResizeFcn(obj)
% get size of panel in pixels
sz = obj.getPixelPos(obj.panel);
% position the axes object
sz(1) = sz(1) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % left
sz(3) = sz(3) - sz(1); % width
sz(2) = sz(2) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH; % bottom
sz(4) = sz(4) - sz(2) - obj.DEFS.BIGBUTTONSIZE - 3*obj.DEFS.PADDING; % height
set(obj.axes,'Position',max(1,sz),'Units','Pixels');
% get size of axes in pixels
sz = obj.getPixelPos(obj.axes);
% figure out where the y middle is
mid = sz(4)/2 + sz(2);
% position the buttons
for i=1:numel(obj.ybuts)
set(obj.ybuts(i),'Position',...
max(1,[obj.DEFS.PADDING, ...
mid+(i-numel(obj.ybuts)/2-1)*obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE]));
end
% figure out where the x middle is
mid = sz(3)/2 + sz(1);
% position the buttons
for i=1:numel(obj.tbuts)
set(obj.tbuts(i),'Position',...
max(1,[mid+(i-numel(obj.tbuts)/2-1)*obj.DEFS.BIGBUTTONSIZE, ...
sz(2) + sz(4) + obj.DEFS.PADDING, ...
obj.DEFS.BIGBUTTONSIZE, ...
obj.DEFS.BIGBUTTONSIZE]));
end
end
function ivMode(obj)
% stop other things that might have been running
obj.stopAll;
obj.mode = 'iv';
% set default positions
obj.ivSizing;
obj.ivResizeFcn;
% No figure cache necessary
obj.fig_cache = [];
% Show figure
obj.fig.Visible = 'on';
end
function ivSizing(obj)
delete(obj.panel);
obj.panel = uipanel('Parent',obj.fig,'Position',[0 0 1 1]);
delete(obj.axes);
obj.axes(1) = axes('Parent',obj.panel,'Position',[0.05 0.05 0.45 0.90],...
'GridLineStyle','-','XColor', 0.15*[1 1 1],'YColor', 0.15*[1 1 1]);
set(obj.axes(1),'NextPlot','add','XLimMode','manual');
set(obj.axes(1),'XGrid','on','YGrid','on','Tag','Axes','Box','on');
obj.axes(1).YLabel.String = 'Current (nA)';
obj.axes(1).YLabel.Color = 'k';
obj.axes(1).XLabel.String = 'Time (s)';
obj.axes(1).XLabel.Color = 'k';
obj.axes(1).YLim = [-0.5, 0.5];
obj.axes(1).XLim = [0 0.5];
obj.axes(2) = axes('Parent',obj.panel,'Position',[0.55 0.05 0.45 0.90],...
'GridLineStyle','-','XColor', 0.15*[1 1 1],'YColor', 0.15*[1 1 1]);
set(obj.axes(2),'NextPlot','add','XLimMode','manual');
set(obj.axes(2),'XGrid','on','YGrid','on','Tag','Axes','Box','on');
obj.axes(2).YLabel.String = 'Mean Current (nA)';
obj.axes(2).YLabel.Color = 'k';
obj.axes(2).XLabel.String = 'Voltage (mV)';
obj.axes(2).XLabel.Color = 'k';
obj.axes(2).YLim = [-1 1];
obj.axes(2).XLim = [-200 200];
% now make the buttons
% y-axis
obj.ybuts = [];
obj.ybuts(1) = uicontrol('Parent', obj.panel, 'String','<html>-</html>',...
'callback', @(~,~) zoom_y('out'));
obj.ybuts(2) = uicontrol('Parent', obj.panel, 'String','<html>↓</html>',...
'callback', @(~,~) scroll_y('down'));
obj.ybuts(3) = uicontrol('Parent', obj.panel, 'String','<html>R</html>',...
'callback', @(~,~) reset_fig);
obj.ybuts(4) = uicontrol('Parent', obj.panel, 'String','<html>↑</html>',...
'callback', @(~,~) scroll_y('up'));
obj.ybuts(5) = uicontrol('Parent', obj.panel, 'String','<html>+</html>',...
'callback', @(~,~) zoom_y('in'));
function zoom_y(str)
if strcmp(str,'in')
lims = 1/2*get(obj.axes(1),'YLim');
elseif strcmp(str,'out')
lims = 2*get(obj.axes(1),'YLim');
end
obj.axes(1).YLim = lims;
obj.axes(2).YLim = lims;
end
function scroll_y(str)
if strcmp(str,'up')
lims = get(obj.axes(1),'YLim') + [1 1]*diff(get(obj.axes(1),'YLim'))/5;
elseif strcmp(str,'down')
lims = get(obj.axes(1),'YLim') - [1 1]*diff(get(obj.axes(1),'YLim'))/5;
end
obj.axes(1).YLim = lims;
obj.axes(2).YLim = lims;
end
function reset_fig
obj.axes(1).YLim = [-1 1];
obj.axes(2).YLim = [-1 1];
obj.axes(1).XLim = [0 0.4];
end
% top
obj.tbuts = [];
obj.tbuts(1) = uicontrol('Parent', obj.panel, ...
'Style', 'togglebutton', 'CData', imread('Play.png'),...
'callback', @(src,~) obj.stateDecision(src), 'tag', 'iv');
% set the resize function
set(obj.panel, 'ResizeFcn', @(~,~) obj.ivResizeFcn);
% and call it to set default positions
obj.ivResizeFcn;
% Show figure
obj.fig.Visible = 'on';
end
function ivResizeFcn(obj)
% position the axes1 object
sz = obj.getPixelPos(obj.panel);
sz(1) = sz(1) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % left
sz(3) = sz(3)/2 - obj.DEFS.PADDING - 1.5*obj.DEFS.LABELWIDTH - obj.DEFS.BUTTONSIZE - 2*obj.DEFS.PADDING; % width
sz(2) = sz(2) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH; % bottom
sz(4) = sz(4) - sz(2) - obj.DEFS.BIGBUTTONSIZE - 3*obj.DEFS.PADDING; % height
set(obj.axes(1),'Position',max(1,sz),'Units','Pixels');
set(obj.axes(1),'Position',max(1,sz),'Units','Pixels');
% figure out where the y middle is
mid = sz(4)/2 + sz(2);
% position the buttons
for i=1:numel(obj.ybuts)
set(obj.ybuts(i),'Position',...
[obj.DEFS.PADDING, ...
mid+(i-numel(obj.ybuts)/2-1)*obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE]);
end
% position the axes2 object
sz = obj.getPixelPos(obj.panel);
sz(1) = sz(1) + sz(3)/2 + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % left
sz(3) = sz(3)/2 - obj.DEFS.PADDING - 1.5*obj.DEFS.LABELWIDTH - obj.DEFS.BUTTONSIZE - 2*obj.DEFS.PADDING; % width
sz(2) = sz(2) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH; % bottom
sz(4) = sz(4) - sz(2) - obj.DEFS.BIGBUTTONSIZE - 3*obj.DEFS.PADDING; % height
set(obj.axes(2),'Position',max(1,sz),'Units','Pixels');
% position the top button
bottom = sz(2) + sz(4) + obj.DEFS.PADDING;
sz = obj.getPixelPos(obj.panel);
mid = sz(1)+sz(3)/2;
for i=1:numel(obj.tbuts)
set(obj.tbuts(i),'Position',...
max(1,[mid+(i-numel(obj.tbuts)/2-1)*obj.DEFS.BIGBUTTONSIZE, ...
bottom, ...
obj.DEFS.BIGBUTTONSIZE, ...
obj.DEFS.BIGBUTTONSIZE]));
end
end
function sealtestMode(obj)
% stop other things that might have been running
obj.stopAll;
obj.mode = 'sealtest';
% set default positions
obj.sealtestSizing;
obj.sealtestResizeFcn;
% no figure cache
obj.fig_cache = [];
% Show figure
obj.fig.Visible = 'on';
end
function sealtestSizing(obj)
delete(obj.panel);
obj.panel = uipanel('Parent',obj.fig,'Position',[0 0 1 1]);
delete(obj.axes);
obj.axes = axes('Parent',obj.panel,'Position',[0.05 0.05 0.95 0.90],...
'GridLineStyle','-','XColor', 0.15*[1 1 1],'YColor', 0.15*[1 1 1]);
set(obj.axes,'NextPlot','replacechildren','XLimMode','manual');
set(obj.axes,'XGrid','on','YGrid','on','Tag','Axes','Box','on');
obj.axes.YLabel.String = 'Current (pA)';
obj.axes.YLabel.Color = 'k';
obj.axes.XLabel.String = 'Time (s)';
obj.axes.XLabel.Color = 'k';
obj.axes.YLim = [-100, 100];
obj.axes.XLim = [0, 2/60];
% now make the buttons
% y-axis
obj.ybuts = [];
obj.ybuts(1) = uicontrol('Parent', obj.panel, 'String','<html>-</html>',...
'callback', @(~,~) zoom_y('out'));
obj.ybuts(2) = uicontrol('Parent', obj.panel, 'String','<html>↓</html>',...
'callback', @(~,~) scroll_y('down'));
obj.ybuts(3) = uicontrol('Parent', obj.panel, 'String','<html>R</html>',...
'callback', @(~,~) reset_fig);
obj.ybuts(4) = uicontrol('Parent', obj.panel, 'String','<html>↑</html>',...
'callback', @(~,~) scroll_y('up'));
obj.ybuts(5) = uicontrol('Parent', obj.panel, 'String','<html>+</html>',...
'callback', @(~,~) zoom_y('in'));
function zoom_y(str)
if strcmp(str,'in')
obj.axes.YLim = 1/2*get(obj.axes(1),'YLim');
elseif strcmp(str,'out')
obj.axes.YLim = 2*get(obj.axes(1),'YLim');
end
end
function scroll_y(str)
if strcmp(str,'up')
obj.axes.YLim = get(obj.axes(1),'YLim') + [1 1]*diff(get(obj.axes(1),'YLim'))/5;
elseif strcmp(str,'down')
obj.axes.YLim = get(obj.axes(1),'YLim') - [1 1]*diff(get(obj.axes(1),'YLim'))/5;
end
end
function reset_fig
obj.axes.YLim = [100, 100];
end
% top
obj.tbuts = [];
obj.tbuts(1) = uicontrol('Parent', obj.panel, ...
'Style', 'togglebutton', 'CData', imread('Play.png'),...
'callback', @(src,~) obj.stateDecision(src), 'tag', 'sealtest');
% set the resize function
set(obj.panel, 'ResizeFcn', @(~,~) obj.sealtestResizeFcn);
% and call it to set default positions
obj.sealtestResizeFcn;
% Show figure
obj.fig.Visible = 'on';
end
function sealtestResizeFcn(obj)
% get size of panel in pixels
sz = obj.getPixelPos(obj.panel);
% position the axes object
sz(1) = sz(1) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH + obj.DEFS.BUTTONSIZE; % left
sz(3) = sz(3) - sz(1); % width
sz(2) = sz(2) + obj.DEFS.PADDING + obj.DEFS.LABELWIDTH; % bottom
sz(4) = sz(4) - sz(2) - obj.DEFS.BIGBUTTONSIZE - 3*obj.DEFS.PADDING; % height
set(obj.axes,'Position',max(1,sz),'Units','Pixels');
% get size of axes in pixels
sz = obj.getPixelPos(obj.axes);
% figure out where the y middle is
mid = sz(4)/2 + sz(2);
% position the buttons
for i=1:numel(obj.ybuts)
set(obj.ybuts(i),'Position',...
max(1,[obj.DEFS.PADDING, ...
mid+(i-numel(obj.ybuts)/2-1)*obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE, ...
obj.DEFS.BUTTONSIZE]));
end
% figure out where the x middle is
mid = sz(3)/2 + sz(1);
% position the buttons
for i=1:numel(obj.tbuts)
set(obj.tbuts(i),'Position',...
max(1,[mid+(i-numel(obj.tbuts)/2-1)*obj.DEFS.BIGBUTTONSIZE, ...
sz(2) + sz(4) + obj.DEFS.PADDING, ...
obj.DEFS.BIGBUTTONSIZE, ...
obj.DEFS.BIGBUTTONSIZE]));
end
end
function writeFileHeader(obj, fid)
% write a file header
h.si = 1/obj.sampling;
h.chNames = {'Current (pA)','Voltage (mV)'};
h.data_compression_scaling = obj.data_compression_scaling;
hh = getByteStreamFromArray(h);
n = numel(hh);
fwrite(fid,n,'*uint32');
fwrite(fid,hh,'*uint8');
end
function writeFileData(obj, fid, data)
fwrite(fid,int16(repmat(obj.data_compression_scaling,size(data,2),1)' .* data),'*int16');
end
function stateDecision(obj, src)
% state machine for the main button presses
if strcmp(get(src,'tag'),'play')
button_state = get(src,'Value');
if button_state == get(src,'Max')
% we are in play mode
recbutton = findobj(obj.tbuts,'tag','record');
obj.stopRecord(recbutton);
set(recbutton,'Value',0);
obj.play(src);
else
% we are stopped
obj.stopPlay(src);
end
elseif strcmp(get(src,'tag'),'record')
button_state = get(src,'Value');
if button_state == get(src,'Max')
% we are in record mode
playbutton = findobj(obj.tbuts,'tag','play');
obj.stopPlay(playbutton);
set(playbutton,'Value',0);
obj.record(src);
else
% we are stopped
obj.stopRecord(src);
end
elseif strcmp(get(src,'tag'),'noise')
button_state = get(src,'Value');
if button_state == get(src,'Max')
% we are in noise display mode
obj.startNoiseDisplay(src);
else
% we are stopped
obj.stopNoiseDisplay(src);
end
elseif strcmp(get(src,'tag'),'iv')
button_state = get(src,'Value');
if button_state == get(src,'Max')
% we are in noise display mode
obj.startIV(src);
else
% we are stopped
obj.stopIV(src);
end
elseif strcmp(get(src,'tag'),'sealtest')
button_state = get(src,'Value');
if button_state == get(src,'Max')
% we are in seal test mode
obj.startSealTest(src);
else
% we are stopped
obj.stopSealTest(src);
end
end
end
function play(obj, button)
% begins displaying data live from the DAQ
try
set(button,'CData',imread('Pause.png'));
set(button,'String','');
catch ex
set(button,'String','Pause');
end
try
obj.fig_cache.clear_fig;
% add listener for data
obj.DAQ.listeners.plot = addlistener(obj.DAQ.s, 'DataAvailable', ...
@(~,event) obj.showData(event));
% start DAQ session
obj.DAQ.s.startBackground;
catch ex
display('Error obtaining data from DAQ.')
end
end
function stopPlay(obj, button)
% Data viewing off
try
set(button,'CData',imread('Play.png'));
set(button,'String','');
catch ex
set(button,'String','Play');
end
try
obj.DAQ.s.stop;
delete(obj.DAQ.listeners.plot);
catch ex
end
end
function record(obj, button)
% begins displaying data live from the DAQ
% and records it to a file in the designated save location
try
set(button,'CData',imread('Recording.png'));
set(button,'String','');