-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXeRay2.m
2118 lines (1600 loc) · 85.6 KB
/
XeRay2.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 XeRay2 < handle
properties
data
gui
const
handles
call
config
ElementProfiles
end
methods
function this = XeRay2(filenames)
initializeGui();
if nargin == 1
this.control('load-file', filenames);
end
function initializeGui()
parent = getParentDir(which('XeRayGUI.m'));
this.config = loadjson(fullfile(parent, 'support-files/xeray-config.json'));
this.config.ScatteringFactorFolder = fullfile(parent, this.config.ScatteringFactorFolder);
this.ElementProfiles = loadjson(fullfile(getParentDir(which('XeRayGUI.m')), 'support-files/element-profiles.json'));
set(0, 'units', this.config.units);
pix = get(0, 'screensize');
if pix(4) * 0.85 <= this.config.window(4)
this.config.window(4) = pix(4)*0.85;
end
this.handles = figure('Visible','on','Name','XeRay','NumberTitle','off','Units','pixels', 'Position', this.config.window, 'Resize', 'on');
this.const = XeRayControl();
createController();
createView();
connectViewAndController();
this.control('initialize');
end
function createController()
%% callbacks - left panel
this.call.file = @FileList_Callback;
this.call.angle = @AngleList_Callback;
this.call.delete = @DeleteButton_Callback;
this.call.load = @LoadButton_Callback;
function FileList_Callback(varargin)
this.control('file');
end
function AngleList_Callback(varargin)
this.control('angle');
end
function DeleteButton_Callback(varargin)
this.control('delete-file');
end
function LoadButton_Callback(varargin)
this.control('load-file');
end
%% callbacks - right panel
this.call.showCal = @ShowCal_Callback;
this.call.basicInfo = @BasicInfo_Callback;
this.call.likelihoodChi2 = @LikelihoodChi2_Callback;
this.call.showFit = @ShowFit_Callback;
this.call.confidence = @ConfidenceInput_Callback;
function ShowCal_Callback(varargin)
this.control('show-cal');
end
function BasicInfo_Callback(~, eventdata)
this.control('basic-info', eventdata);
end
function LikelihoodChi2_Callback(varargin)
this.view('likelihood-chi2');
end
function ShowFit_Callback(varargin)
this.control('show-fit');
end
function ConfidenceInput_Callback(varargin)
this.control('confidence-input');
end
%% callbacks - table related
this.call.layer = @LayerTable_Callback;
this.call.addLayer = @AddLayer_Callback;
this.call.deleteLayers = @DeleteLayers_Callback;
this.call.parametersTable = @ParametersTable_Callback;
function LayerTable_Callback(~, eventdata)
this.control('layer-table', eventdata);
end
function AddLayer_Callback(varargin)
this.control('add-layer');
end
function DeleteLayers_Callback(varargin)
% delete the layer from layer table
this.control('delete-layers');
end
function ParametersTable_Callback(~, eventdata)
this.control('parameter-table', eventdata);
end
%% callbacks - fitting related
this.call.startFitting = @StartFitting_Callback;
this.call.loadParameters = @LoadParameters_Callback;
this.call.saveParameters = @SaveParameters_Callback;
this.call.stepInput = @StepInput_Callback;
this.call.fit = @FitButton_Callback;
this.call.updateStarts = @UpdateStartButton_Callback;
function StartFitting_Callback(varargin)
this.control('start-fitting');
end
function LoadParameters_Callback(varargin)
this.control('load-parameters');
end
function SaveParameters_Callback(varargin)
this.control('save-parameters');
end
function StepInput_Callback(varargin)
this.control('step-input');
end
function FitButton_Callback(varargin)
this.control('fit');
end
function UpdateStartButton_Callback(varargin)
this.control('update-start');
end
%% callbacks - saving functions
this.call.saveOutput = @SaveOutputTextButton_Callback;
this.call.saveUpperFigure = @SaveUpperFigureButton_Callback;
this.call.saveLowerFigure = @SaveLowerFigureButton_Callback;
this.call.saveDataAndFit = @SaveDataAndFitButton_Callback;
this.call.clear = @ClearButton_Callback;
this.call.record = @RecordFittingButton_Callback;
function SaveOutputTextButton_Callback(varargin) %save text output
this.control('save-output');
end
function SaveUpperFigureButton_Callback(varargin) %save figure one
this.control('save-upper-figure');
end
function SaveLowerFigureButton_Callback(varargin) %save figure one
this.control('save-lower-figure');
end
function SaveDataAndFitButton_Callback(varargin)
this.control('save-data');
end
function ClearButton_Callback(varargin)
this.control('clear-output');
end
function RecordFittingButton_Callback(varargin)
this.control('record-results');
end
end
function createView()
handle0 = this.handles;
createListPanel();
createAxes();
createRightPanel();
createBasicInfoTalbe();
createParametersTable();
createLayersTable();
createFittingControls();
createOutputandSaveButtons();
function createListPanel()
listPanel = uipanel(handle0,'Title','X-ray Fluorescence Data','Units','normalized',...
'Position',[0.014 0.02 0.16 0.97]);
this.gui.scanText = uicontrol(listPanel,'Style','text','String','Select data sets to begin','Units','normalized',...
'Position',[0.05 0.965 0.8 0.03]);
this.gui.fileList = uicontrol(listPanel,'Style','listbox','Units','normalized',...
'Position',[0.05 0.56 0.9 0.405],'Max',2);
this.gui.loadButton = uicontrol(listPanel,'Style','pushbutton','String','Load','Units','normalized',...
'Position',[0.035 0.52 0.3 0.032]);
this.gui.deleteButton = uicontrol(listPanel,'Style','pushbutton','String','Delete','Units','normalized',...
'Position',[0.38 0.52 0.3 0.032]);
uicontrol(listPanel,'Style','text','String','Select angle range','Units','normalized',...
'Position',[0.05 0.49 0.8 0.03]);
this.gui.angleList = uicontrol(listPanel,'Style','listbox','Units','normalized',...
'Position',[0.05 0.015 0.9 0.48],'Max',2);
end
function createAxes()
this.gui.startFitting = uicontrol(handle0,'Style','checkbox','String','Start Fitting','Units','normalized','Visible','on',...
'Position',[0.6 0.965 0.1 0.018]);
this.gui.likelihoodChi2 = uicontrol(handle0,'Style','popupmenu','String',{'Likelihood','Chi^2'},'Visible','off',...
'Units','normalized',...
'Position',[0.572 0.97 0.1 0.018]);
this.gui.showCal = uicontrol(handle0,'Style','checkbox','String','Show Calc.','Units','normalized',...
'Position',[0.6 0.437 0.08 0.018]);
this.gui.showFit = uicontrol(handle0,'Style','checkbox','String','Show Fit','Units','normalized',...
'Position',[0.54 0.437 0.06 0.018]);
ax1 = axes('Parent',handle0,'Units','normalized','Position',[0.215 0.52 0.45 0.44]);
ax1.XLim = [0 10];
ax1.YLim = [0 10];
ax1.XTick = [0 2 4 6 8 10];
ax1.YTick = [0 2 4 6 8 10];
ax1.XLabel.String = 'x1';
ax1.YLabel.String = 'y1';
this.gui.ax1 = ax1;
% plot region 2
ax2 = axes('Parent',handle0,'Units','normalized','Position',[0.215 0.08 0.45 0.35]);
ax2.XLim = [0 10];
ax2.YLim = [0 10];
ax2.XTick = [0 2 4 6 8 10];
ax2.YTick = [0 2 4 6 8 10];
ax2.XLabel.String = 'x2';
ax2.YLabel.String = 'y2';
this.gui.ax2 = ax2;
end
function createRightPanel()
this.gui.rightPanel = uipanel(handle0,'Units','normalized','Position',[0.68 0.02 0.31 0.97]);
this.gui.elementEditPanel = uipanel(handle0,'Title', 'Element Management', 'Visible', 'off', 'Units', 'normalized', 'Position',[0.685 0.03 0.3 0.95]);
end
function createBasicInfoTalbe()
rightPanel = this.gui.rightPanel;
rowName = {'Beam Energy (keV)', 'Emission Energy (kev)', 'Slit Size (mm)', 'Detector Footprint (mm)'};
colName = {};
columnFormat = {'numeric'};
columnWidth = {120};
tableData = {20; 14.164; 0.02; 13};
this.gui.basicInfoTable = uitable(rightPanel, 'Data', tableData, 'ColumnName', colName, ...
'ColumnFormat', columnFormat, 'ColumnEditable', true, 'Units','normalized', ...
'ColumnWidth',columnWidth,'RowName',rowName, 'RowStriping','off',...
'Position', [0.025 0.88 0.935 0.1], 'TooltipString', 'Press enter to update value.');
end
function createLayersTable()
rightPanel = this.gui.rightPanel;
rowName = {'top', 'bottom'};
colName = {'Formula', 'ED', 'Depth (A)', 'Delete'};
colFormat = {'char', 'numeric', 'numeric', 'logical'};
colWidth = {130, 40, 60, 50};
%heliumEd = 101325 / 8.314 / 298 / 1e3 * 6.02e23 * 1e-27 * 2;
tableData = {'CH2', 0.26, Inf, false; 'H2O', 0.334, Inf, false};
base = 0.85;
uicontrol(rightPanel,'Style','text','String','Layer Structure:','Units','normalized','HorizontalAlignment','left', 'Position',[0.025 base 0.8 0.025]);
this.gui.layerTable = uitable(rightPanel,'Data', tableData,'ColumnName', colName,...
'ColumnFormat', colFormat,'ColumnEditable', true(1, 6), 'Units', 'normalized',...
'ColumnWidth',colWidth,'RowName',rowName,'RowStriping','off',...
'Position', [0.025 base-0.15 0.935 0.15]);
this.gui.addLayer = uicontrol(rightPanel,'Style','pushbutton','String', 'Add', 'Units','normalized', 'Position', [0.725 base-0.18 0.11 0.03]);
this.gui.deleteLayer = uicontrol(rightPanel,'Style','pushbutton','String', 'Delete','Units','normalized', 'Position', [0.84 base-0.18 0.12 0.03]);
end
function createParametersTable()
rightPanel = this.gui.rightPanel;
rowName = {'Angle-Offset','Scale-Factor','Background', 'Conc-bottom'};
colName = {'Min','Max','Start','Fix','Plot'};
colFormat = {'numeric','numeric','numeric','logical','logical'};
colWidth = {55 55 55 30 30};
tableData = {-0.0001, 0.0001, 0, false, false; 1, 1, 1, true, false; 1, 1, 1, true, false; 0, 0, 0, true, false};
base = 0.665;
this.gui.parametersTableTitle = uicontrol(rightPanel,'Style','text','String','Fitting Parameters:','Units','normalized','HorizontalAlignment','left', 'Position', [0.025 base 0.8 0.025]);
this.gui.parametersTable = uitable(rightPanel,'Data', tableData,'ColumnName', colName,...
'ColumnFormat', colFormat,'ColumnEditable', [true true true true true],'Units','normalized',...
'ColumnWidth',colWidth,'RowName',rowName,'RowStriping','off', 'Position', [0.025 base-0.2 0.935 0.2]);
end
function createFittingControls()
rightPanel = this.gui.rightPanel;
base = 0.43;
this.gui.layerTableTitle = uicontrol(rightPanel,'Style','text','String', 'Fitting Control:','Units','normalized','HorizontalAlignment','left',...
'Position',[0.025 base 0.8 0.025]);
this.gui.loadPara = uicontrol(rightPanel,'Style','pushbutton','String','Load Para','Units','normalized',...
'Position',[0.024 base-0.03 0.17 0.03]);
this.gui.savePara = uicontrol(rightPanel,'Style','pushbutton','String','Save Para','Units','normalized',...
'Position',[0.19 base-0.03 0.17 0.03]);
this.gui.stepInput = uicontrol(rightPanel,'Style','edit','String',20,'Units','normalized',...
'HorizontalAlignment','left','Position',[0.62 base-0.03 0.1 0.03]);
this.gui.stepText = uicontrol(rightPanel,'Style','text','String','Steps','Units','normalized',...
'HorizontalAlignment','left','Position', [0.735 base-0.035 0.08 0.03]);
this.gui.fitButton = uicontrol(rightPanel,'Style','pushbutton','String','Fit','Units','normalized',...
'Position',[0.82 base-0.03 0.15 0.03]);
this.gui.withText = uicontrol(rightPanel,'Style','text','String','With','Units','normalized','HorizontalAlignment','left',...
'Position',[0.025 base-0.065 0.07 0.03]);
this.gui.confidenceInput = uicontrol(rightPanel,'Style','edit','String','95','Units','normalized',...
'HorizontalAlignment','left','Position',[0.1 base-0.06 0.07 0.03]);
this.gui.confidenceText = uicontrol(rightPanel,'Style','text','String','% confidence window','Units','normalized','HorizontalAlignment','left',...
'Position',[0.171 base-0.065 0.28 0.03]);
this.gui.recordFitting = uicontrol(rightPanel,'Style','pushbutton','String','Record Fitting','Units','normalized',...
'Position',[0.452 base-0.06 0.22 0.03]);
this.gui.updateStartButton = uicontrol(rightPanel,'Style','pushbutton','String','Update Starts','Units','normalized',...
'Position',[0.75 base-0.06 0.22 0.03]);
end
function createOutputandSaveButtons()
rightPanel = this.gui.rightPanel;
this.gui.output = uicontrol(rightPanel,'Style','edit','Max',2,'HorizontalAlignment','left','Units','normalized',...
'Position',[0.03 0.07 0.935 0.29]);
this.gui.clearOutput = uicontrol(rightPanel,'Style','pushbutton','String','Clear','Units','normalized',...
'Position',[0.82 0.038 0.15 0.03]);
uicontrol(rightPanel,'Style','text','String','Save:','Units','normalized',...
'HorizontalAlignment','left','Position',[0.025 0.035 0.08 0.025]);
this.gui.saveOutput = uicontrol(rightPanel,'Style','pushbutton','String','Output Text','Units','normalized',...
'Position',[0.024 0.007 0.2 0.03]);
this.gui.saveUpperFigure = uicontrol(rightPanel,'Style','pushbutton','String','Upper Figure','Units','normalized',...
'Position',[0.234 0.007 0.2 0.03]);
this.gui.saveLowerFigure = uicontrol(rightPanel,'Style','pushbutton','String','Lower Figure','Units','normalized',...
'Position',[0.444 0.007 0.2 0.03]);
this.gui.saveData = uicontrol(rightPanel,'Style','pushbutton','String','Data & Fit','Units','normalized',...
'Position',[0.66 0.007 0.17 0.03]);
end
end
function connectViewAndController()
% left panel
this.gui.fileList.Callback = this.call.file;
this.gui.loadButton.Callback = this.call.load;
this.gui.deleteButton.Callback = this.call.delete;
this.gui.angleList.Callback = this.call.angle;
% middle panel
this.gui.likelihoodChi2.Callback = this.call.likelihoodChi2;
this.gui.showFit.Callback = this.call.showFit;
this.gui.showCal.Callback = this.call.showCal;
this.gui.startFitting.Callback = this.call.startFitting;
% table callbacks
this.gui.basicInfoTable.CellEditCallback = this.call.basicInfo;
this.gui.layerTable.CellEditCallback = this.call.layer;
this.gui.addLayer.Callback = this.call.addLayer;
this.gui.deleteLayer.Callback = this.call.deleteLayers;
this.gui.parametersTable.CellEditCallback = this.call.parametersTable;
% fitting controls
this.gui.loadPara.Callback = this.call.loadParameters;
this.gui.savePara.Callback = this.call.saveParameters;
this.gui.recordFitting.Callback = this.call.record;
this.gui.fitButton.Callback = this.call.fit;
this.gui.stepInput.Callback = this.call.stepInput;
this.gui.confidenceInput.Callback = this.call.confidence;
this.gui.updateStartButton.Callback = this.call.updateStarts;
% output and save
this.gui.saveData.Callback = this.call.saveDataAndFit;
this.gui.saveUpperFigure.Callback = this.call.saveUpperFigure;
this.gui.saveLowerFigure.Callback = this.call.saveLowerFigure;
this.gui.saveOutput.Callback = this.call.saveOutput;
this.gui.clearOutput.Callback = this.call.clear;
end
end
function feedback = model(this, state, trigger, varargin)
switch state
case 'empty'
switch trigger
case 'load-file'
files = varargin{1};
paths = varargin{2};
loadNewData(files, paths);
end
case 'explore'
switch trigger
case 'delete-file'
deleteSelectedFiles();
case 'load-file'
files = varargin{1};
path = varargin{2};
loadNewData(files, path);
case 'delete-layers'
this.processInputs('layer');
case 'inputs'
what = varargin{1};
this.processInputs(what);
case 'switch-element'
newElement = varargin{1};
switch newElement
case 'none'
% do nothing
case 'new'
otherwise
if ~strcmp(newElement, this.const.element)
this.const.element = newElement;
fitSpectraToElement();
end
end
end
case 'fitting'
switch trigger
case 'start-fitting'
end
end
% model functions
function deleteSelectedFiles()
fileList = this.gui.fileList;
if ~isempty(fileList.String)
if isa(fileList.String, 'char')
n = true;
else
n = true(1, length(fileList.String));
end
m = fileList.Value;
n(m) = false(1, length(m));
this.data = this.data(n);
feedback = n;
end
end
function loadNewData(files, paths)
if ~isempty(files)
% convert to cell array
if isa(files, 'char')
files = {files};
paths = {paths};
else
if isa(paths, 'char')
paths = repmat({paths}, 1, length(files));
end
end
% load data
n = length(files);
newData = cell(1, n);
for i = 1:n
newData{i} = XeSignal(fullfile(paths{i}, files{i}), this.config.ScatteringFactorFolder);
end
this.data = [this.data, newData];
end
end
function fitSpectraToElement()
element = this.const.element;
lineshape = this.const.lineShape;
for i = 1 : length(this.data)
this.data{i}.selectElement(element, lineshape);
end
end
end
function view(this, state, trigger, varargin)
switch state
case 'empty'
switch trigger
case 'initialize'
switchToExplore();
case 'load-file'
olds = varargin{1};
news = varargin{2};
this.gui.fileList.String = [olds; news];
displayAngles();
replot('both');
this.gui.startFitting.Enable = 'on';
end
case 'explore'
switch trigger
case 'file'
displayAngles();
replot('lower');
case 'angle'
replot('lower');
case 'delete-file'
indices = varargin{1};
deleteSelectedFiles(indices);
replot('lower');
case 'load-file'
olds = varargin{1};
news = varargin{2};
this.gui.fileList.String = [olds; news];
case 'start-fitting'
switchElementFitting('off');
replot('lower');
otherwise
warning('Case not fonund for XeRayGUI.view() - whole-spectra state.');
end
case 'fitting'
switch trigger
case 'angle'
replot('both');
case 'start-fitting'
switchElementFitting('on');
case 'show-cal'
replot('lower');
case 'layer-table'
replot('lower');
case 'basic-info'
replot('lower');
case 'parameter-table'
what = varargin{1};
replot(what);
case 'add-layer'
addLayer();
case 'add-layer-update'
replot('lower');
case 'delete-layers'
deleteLayers();
case 'delete-layers-update'
replot('lower');
case 'load-parameters'
loadParameters();
case 'load-parameters-update'
replot('lower');
case 'fit'
this.gui.showFit.Enable = 'on';
this.gui.showFit.Value = 1;
replot('lower');
replot('upper');
recordFittingResults(0);
case 'show-fit'
replot('lower');
case 'update-start'
replot('lower');
case 'clear-output'
this.gui.output.String = {};
case 'record-results'
confidence = str2double(this.gui.confidenceInput.String) / 100;
recordFittingResults(confidence);
case 'confidence-input'
confidence = str2double(this.gui.confidenceInput.String) / 100;
recordFittingResults(confidence);
end
end
% view functions
function deleteSelectedFiles(indices)
fileList = this.gui.fileList;
angleList = this.gui.angleList;
if sum(indices)
fileList.String = fileList.String(indices);
fileList.Value = 1;
angleList.Value = 1;
displayAngles();
else
fileList.Value = 1;
fileList.String = {};
angleList.String = {};
end
end
function switchToExplore()
set(findall(this.gui.rightPanel, '-property', 'Enable'), 'Enable', 'off');
this.gui.fileList.Enable = 'on';
this.gui.showFit.Enable = 'off';
this.gui.showCal.Enable = 'off';
this.gui.startFitting.Enable = 'off';
this.gui.startFitting.Value = 0;
end
function switchElementFitting(status)
if strcmp(status, 'on')
antiStatus = 'off';
else
antiStatus = 'on';
this.gui.showFit.Enable = 'off';
this.gui.showFit.Value = 0;
this.gui.showCal.Value = 0;
end
set(findall(this.gui.rightPanel, '-property', 'Enable'), 'Enable', status);
this.gui.showCal.Enable = status;
this.gui.startFitting.Enable = 'on';
this.gui.fileList.Enable = antiStatus;
this.gui.elementPopup.Enable = antiStatus;
this.gui.lineShape.Enable = antiStatus;
this.gui.removeBackground.Enable = antiStatus;
this.gui.loadButton.Enable = antiStatus;
this.gui.deleteButton.Enable = antiStatus;
end
function loadParameters()
[filename, pathname] = uigetfile('*.xeraypara', 'Load a saved parameter file.');
if ~isnumeric(filename)
file = fullfile(pathname, filename);
para = loadjson(file);
% load the basic info table
this.gui.basicInfoTable.Data = num2cell(para.basic);
% load the layer table
dat = para.layer;
if ~ischar(dat{1})
n = length(dat);
m = length(dat{1});
newdata = cell(n, m);
for i = 1 : n
newdata(i, :) = dat{i};
end
dat = newdata;
end
for i = 1 : size(dat, 1)
dat{i, end} = logical(dat{i, end});
end
this.gui.layerTable.Data = dat;
assignLayerTableRowName();
% load the parameters table
dat = num2cell(para.parameter);
n = size(dat, 1);
for i = 1 : n
dat{i, 4} = logical(dat{i, 4});
dat{i, 5} = logical(dat{i, 5});
end
this.gui.parametersTable.Data = dat;
assignParameterTableRowName();
end
end
function displayAngles()
fileList = this.gui.fileList;
angleList = this.gui.angleList;
n = zeros(size(fileList.Value));
for i = 1:length(fileList.Value)
n(i) = length(this.data{fileList.Value(i)}.rawdata.angle);
end
angleStrings = cell(1, sum(n));
k = 0;
for i = 1:length(n)
for j = 1 : n(i)
k = k + 1;
angleStrings{k} = sprintf('%s %s %f',fileList.String{fileList.Value(i)}(1:end-6), '@angle: ',this.data{fileList.Value(i)}.rawdata.angle(j));
end
end
if max(angleList.Value) > length(angleStrings)
angleList.Value = 1;
end
angleList.String = angleStrings;
end
function assignLayerTableRowName()
table = this.gui.layerTable;
n = size(table.Data, 1);
rowNames = cell(1, n);
for i = 1 : n
rowNames{i} = num2str(i);
end
table.RowName = rowNames;
end
function assignParameterTableRowName()
table = this.gui.parametersTable;
n = size(table.Data, 1);
rowNames = cell(n, 1);
rowNames(1:3) = {'Angle-Offset', 'Scale-Factor', 'Background'};
for i = 4 : n
rowNames{i} = strcat('Conc-', num2str(i-3));
end
table.RowName = rowNames;
end
function n = chosenPlotPara()
n = sum(cell2mat(this.gui.parametersTable.Data(:, end)));
end
function indices = indicesInFit()
dat = cell2mat(this.gui.parametersTable.Data(:, 4:5));
fixed = ~dat(:, 1);
plotting = dat(:, 2);
if sum(plotting)
indices = zeros(1, sum(plotting));
plotting = find(plotting);
for i = 1 : length(plotting)
indices(i) = sum(fixed(1 : plotting(i)));
end
end
end
function [n, m, rankn, rankm] = getSelectionIndex()
% 4 indices for each selected spectrum
% n is the position within the x{} data set, m is the position for
% the angle, rankn is the position of n within selected n, and rankm is
% the position of m within selected m for that specific n
fileList = this.gui.fileList;
angleList = this.gui.angleList;
n = zeros(size(angleList.Value));
m = n;
rankn = n;
rankm = n;
cn = cumsum(dataLengths(fileList.Value));
for i = 1:length(angleList.Value)
rankn(i) = find(cn >= angleList.Value(i),1);
n(i) = fileList.Value(rankn(i));
if rankn(i) > 1
m(i) = angleList.Value(i) - cn(rankn(i)-1);
else
m(i) = angleList.Value(i);
end
if i == 1 || rankn(i) > rankn(i-1)
rankm(i) = 1;
else
rankm(i) = rankm(i-1)+1;
end
end
end
function [fileIndex, angleIndices] = getSelectedSignalIndices()
[n, m, ~, ~] = getSelectionIndex();
n1 = n;
n1(2:end) = n1(2:end)-n1(1:end-1);
ind = find(n1~=0);
fileIndex = zeros(size(ind));
angleIndices = cell(size(ind));
for i = 1:length(ind)-1
fileIndex(i) = n(ind(i));
angleIndices{i} = m(ind(i):ind(i+1)-1);
end
fileIndex(end) = n(ind(end));
angleIndices{end} = m(ind(end):end);
end
function [styles1, legends1, styles2, legends2] = getSpectraStylesAndLegends()
angleList = this.gui.angleList;
fileList = this.gui.fileList;
%obtain line spec for plotting, and legends
styles1 = cell(size(angleList.Value));
legends1 = styles1;
[~,~,rankn,rankm] = getSelectionIndex();
rankn = mod(rankn,length(this.const.symbols));
rankn(rankn==0) = length(this.const.symbols);
rankm = mod(rankm,length(this.const.colors));
rankm(rankm==0) = length(this.const.colors);
for i = 1:length(angleList.Value)
styles1{i} = strcat(this.const.symbols(rankn(i)),this.const.colors(rankm(i)));
legends1{i} = angleList.String{angleList.Value(i)};
end
uniqueN = unique(rankn,'stable');
styles2 = cell(size(uniqueN));
legends2 = styles2;
for i = 1:length(uniqueN)
styles2{i} = this.const.symbols(uniqueN(i));
legends2{i} = fileList.String{fileList.Value(uniqueN(i))};
end
end
function lengths = dataLengths(indices)
if ~isempty(this.data)
if nargin == 1
indices = 1 : length(this.data);
elseif max(indices) > length(this.data)
warning('Indices cannot be larger than the number of datasets.');
end
n = length(indices);
lengths = zeros(1, n);
for i = 1 : n
lengths(i) = length(this.data{indices(1)}.rawdata.angle);
end
else
lengths = 0;
disp('No data.');
end
end
function starts = obtainStarts()
mat = cell2mat(this.gui.parametersTable.Data(:, 1:3));
starts = mat(:, 3)';
end
function addLayer()
this.gui.showFit.Value = false;
this.gui.showFit.Enable = 'off';
% update layers table
table = this.gui.layerTable;
table.Data = [table.Data(1, :); {'H2O', 0.334, 1, false}; table.Data(2:end, :)];
n = size(table.Data, 1) -2;
layerName = strcat('layer-', num2str(n));
table.RowName = [table.RowName(1); layerName; table.RowName(2:end)];