-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnalySize.m
2067 lines (1544 loc) · 68.5 KB
/
AnalySize.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
function varargout = AnalySize(varargin)
% ANALYSIZE MATLAB code for AnalySize.fig
% ANALYSIZE, by itself, creates a new ANALYSIZE or raises the existing
% singleton*.
%
% H = ANALYSIZE returns the handle to a new ANALYSIZE or the handle to
% the existing singleton*.
%
% ANALYSIZE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ANALYSIZE.M with the given input arguments.
%
% ANALYSIZE('Property','Value',...) creates a new ANALYSIZE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before AnalySize_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to AnalySize_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help AnalySize
% Last Modified by Greig Paterson 04-Jul-2020
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @AnalySize_OpeningFcn, ...
'gui_OutputFcn', @AnalySize_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before AnalySize is made visible.
function AnalySize_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to AnalySize (see VARARGIN)
% Choose default command line output for AnalySize
handles.output = hObject;
% UIWAIT makes AnalySize wait for user response (see UIRESUME)
% uiwait(handles.AnalySize_MW);
%% Get some system info and set some defaults
% Get the MATLAB version
Ver = ver('MATLAB');
handles.Version = str2double(Ver.Version);
handles.AnalySize_Version = '1.2.2';
handles.AnalySize_Date = 'May 2022';
handles.Curent_Pos = get(handles.AnalySize_MW, 'Position');
% Get the screen dpi
set(0, 'Units', 'Pixels');
Sp = get(0, 'ScreenSize');
handles.Screen_Res = Sp(3:4);
try
handles.Screen_DPI = get(0, 'ScreenPixelsPerInch');
catch %#ok<CTCH>
set(0, 'Units', 'Inches');
Si = get(0, 'ScreenSize');
set(0, 'Units', 'Pixels');
dpi = Sp./Si;
handles.Screen_DPI = mean(dpi(3:4));
end
% Set the position to about mid screen
currentPosition = get(handles.AnalySize_MW, 'Position');
newX = Sp(1) + (Sp(3)/2 - currentPosition(3)/2);
newY = Sp(2) + (Sp(4)/2 - currentPosition(4)/2);
newW = currentPosition(3);
newH = currentPosition(4);
set(handles.AnalySize_MW, 'Position', [newX, newY, newW, newH]);
% Get the OS and line endings for outputting files
if ispc
handles.OS = 'Win';
handles.Line_End = '\r\n';
elseif ismac
handles.OS = 'Mac';
handles.Line_End = '\n';
elseif isunix
handles.OS = 'Unix';
handles.Line_End = '\n';
else
handles.OS = 'Unknown';
handles.Line_End = '\n';
end
% Get the user defaults
% Get the current path of the main m-file
S = mfilename('fullpath');
name_len = length(mfilename());
MyPath = S(1:end-name_len);
Defaults = Read_Config_File(MyPath);
% Get the defaults needed for the main window
handles.Default_Plot_Colors = Defaults.EM_Plot_Color;
handles.Default_Data_Color = Defaults.Data_Plot_Color;
handles.Default_Data_Symbol = Defaults.DataSymbol;
handles.Default_Data_Symbol_Size = Defaults.DataSymbolSize;
if strcmpi(Defaults.DataFaceColor, 'filled')
handles.Default_Data_Symbol_Fill = handles.Default_Data_Color;
else
handles.Default_Data_Symbol_Fill = 'none';
end
% set(handles.PDF_Axes, 'ColorOrder', handles.Default_Plot_Colors);
% set(handles.EM_Axes, 'ColorOrder', handles.Default_Plot_Colors);
func_handles = SetDefaultHandles(handles, 'All');
handles = func_handles;
% Set key data to appdata
setappdata(handles.AnalySize_MW, 'LineEnd', handles.Line_End);
setappdata(handles.AnalySize_MW, 'OS', handles.OS);
setappdata(handles.AnalySize_MW, 'Resolution', handles.Screen_Res);
setappdata(handles.AnalySize_MW, 'DPI', handles.Screen_DPI);
setappdata(handles.AnalySize_MW, 'Defaults', Defaults );
setappdata(handles.AnalySize_MW, 'Version', handles.Version);
% Set window names for closing function
handles.WindowNames = [{'About AnalySize'}; {'Censor Data'}; {'CM Plot'}; {'Descriptive Statistics'}; {'EM Colors'};...
{'Set Symbol'}; {'Spectra Plot'}; {'Ternary Plots'}];
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = AnalySize_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes when user attempts to close AnalySize_MW.
function AnalySize_MW_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to AnalySize_MW (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.Data_Loaded ~= 0
choice = questdlg('Do you want to save before quitting?', 'Save Session?', 'Yes', 'No', 'Cancel', 'Cancel');
if strcmpi(choice, 'Yes')
MB_Save_Session_Callback(hObject, eventdata, handles);
end
if strcmpi(choice, 'Cancel')
return;
else
nWindows = size(handles.WindowNames, 1);
for ii = 1:nWindows
h = findall(0,'type','figure', 'name', handles.WindowNames{ii});
close(h);
end
delete(hObject);
end
else
% No data loaded, just ask to quit or not
choice = questdlg('Do you want to quit?', 'Quite AnalySize?', 'Yes', 'No', 'No');
if strcmpi(choice, 'Yes')
nWindows = size(handles.WindowNames, 1);
for ii = 1:nWindows
h = findall(0,'type','figure', 'name', handles.WindowNames{ii});
close(h);
end
delete(hObject);
else
return;
end
end
% --- Set the current data
% set the data for the currently loaded sample
function func_handles = Set_Current_Data(handles)
X = handles.All_Data{handles.spec_ind};
GS=handles.All_GS{:,handles.spec_ind};
LGS=handles.All_LGS{:,handles.spec_ind};
Phi = handles.All_Phi{:,handles.spec_ind};
handles.Current_Data = X;
% handles.Current_Data_CS = cumsum(X);
handles.Current_LGS = LGS;
handles.Current_GS = GS;
handles.Current_Phi = Phi;
% Need to update the endmember fit for SSU
func_handles=Set_Current_Fit(handles);
handles = func_handles;
Update_Plots(handles)
% Save the updated handles to the temporary variable func_handles
func_handles=handles;
% --- Set the currently selected fit
% set the data for the currently selected fit
function func_handles=Set_Current_Fit(handles)
Fit_Ind = handles.Fit_Data_Ind;
spec_ind = handles.spec_ind;
if Fit_Ind ~= 0
handles.Current_Fit_Type = handles.All_Fit_Types{1,Fit_Ind};
handles.Specimen_QFit = handles.All_Specimen_QFit{1, Fit_Ind};
handles.DataSet_QFit = handles.All_DataSet_QFit{1, Fit_Ind};
handles.Current_Fit_EMs = handles.All_Fit_EMs{1,Fit_Ind};
handles.Current_Fit_Abunds = handles.All_Fit_Abunds{1,Fit_Ind};
handles.Current_Fit_Params = handles.All_Fit_Params{1,Fit_Ind};
handles.Current_Fit_PDFs = handles.Current_Fit_Abunds(spec_ind,:) * handles.Current_Fit_EMs;
handles.Current_Specimen_Fit = repmat(handles.Current_Fit_Abunds(handles.spec_ind,:), handles.nVar, 1)'.*handles.Current_Fit_EMs;
handles.nEnd = size(handles.Current_Fit_EMs, 1);
Table_Abunds = 100.*handles.Current_Fit_Abunds;
end
% Update the plots
Update_Plots(handles);
% update the data table
nFits = handles.nEnd;
Cols = handles.Table_Cols;
Tdata = [handles.All_Names, num2cell(handles.Specimen_QFit) ];
for ii = 1:nFits
Cols = [Cols, strcat('EM', sprintf('% d', ii) ) ]; %#ok<AGROW>
Tdata = [Tdata, num2cell(Table_Abunds(:,ii))]; %#ok<AGROW>
end
set(handles.Data_Table, 'Data', Tdata, 'ColumnName', Cols);
func_handles = handles;
% --- update the plots
function Update_Plots(handles)
Plot_Type = get(handles.Plot_Type, 'Value');
Plot_Fits = handles.Plot_Fits_Flag;
% Get the plot symbols
Msymbol = handles.Default_Data_Symbol;
Msize = handles.Default_Data_Symbol_Size;
Mcolor = handles.Default_Data_Color;
Mfill = handles.Default_Data_Symbol_Fill;
FUnits = 'Pixels';
FontSize1 = 12; % 12pt font
FontSize2 = 14; % 14pt font
switch Plot_Type
case 1 % Log Scale
Xplot=handles.Current_LGS;
plot(handles.PDF_Axes, Xplot, 100.*handles.Current_Data, 'Marker', Msymbol, 'MarkerSize', Msize, 'MarkerFaceColor', Mfill, 'Color', Mcolor, 'LineStyle', 'none','LineWidth', 1);
set(get(handles.PDF_Axes, 'XLabel'), 'String', 'Ln(grain size in \mu{m})', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.PDF_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.PDF_Axes, 'Title'), 'Interpreter', 'none');
set(get(handles.PDF_Axes, 'Title'), 'String', handles.All_Names{handles.spec_ind}, 'FontUnits', FUnits, 'FontSize', FontSize2);
case 2 % Log-Linear Scale
Xplot=handles.Current_GS;
plot(handles.PDF_Axes, Xplot, 100.*handles.Current_Data, 'Marker', Msymbol, 'MarkerSize', Msize, 'MarkerFaceColor', Mfill, 'Color', Mcolor, 'LineStyle', 'none', 'LineWidth', 1);
set(handles.PDF_Axes, 'XScale', 'Log');
set(get(handles.PDF_Axes, 'XLabel'), 'String', 'Grain size [\mu{m}]', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.PDF_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.PDF_Axes, 'Title'), 'Interpreter', 'none');
set(get(handles.PDF_Axes, 'Title'), 'String', handles.All_Names{handles.spec_ind}, 'FontUnits', FUnits, 'FontSize', FontSize2);
case 3 % Phi scale
Xplot=handles.Current_Phi;
plot(handles.PDF_Axes, Xplot, 100.*handles.Current_Data, 'Marker', Msymbol, 'MarkerSize', Msize, 'MarkerFaceColor', Mfill, 'Color', Mcolor, 'LineStyle', 'none', 'LineWidth', 1);
set(get(handles.PDF_Axes, 'XLabel'), 'String', '\phi', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.PDF_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.PDF_Axes, 'Title'), 'Interpreter', 'none');
set(get(handles.PDF_Axes, 'Title'), 'String', handles.All_Names{handles.spec_ind}, 'FontUnits', FUnits, 'FontSize', FontSize2);
case 4 % Linear scale
Xplot=handles.Current_GS;
plot(handles.PDF_Axes, Xplot, 100.*handles.Current_Data, 'Marker', Msymbol, 'MarkerSize', Msize, 'MarkerFaceColor', Mfill, 'Color', Mcolor, 'LineStyle', 'none', 'LineWidth', 1);
set(get(handles.PDF_Axes, 'XLabel'), 'String', 'Grain size [\mu{m}]', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.PDF_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.PDF_Axes, 'Title'), 'Interpreter', 'none');
set(get(handles.PDF_Axes, 'Title'), 'String', handles.All_Names{handles.spec_ind}, 'FontUnits', FUnits, 'FontSize', FontSize2);
end
% Set the default color order for the PDF plot
% Do a MATLAB version check
if handles.Version <= 8.3
% 2014a and before
set(handles.PDF_Axes, 'ColorOrder',handles.Default_Plot_Colors);
handles.PDF_Axes.ColorOrderIndex = 1;
else
% add black to top for the total PDF fit
set(handles.PDF_Axes, 'ColorOrder', [[0 0 0]; handles.Default_Plot_Colors]);
handles.PDF_Axes.ColorOrderIndex = 1;
end
if Plot_Fits==1
PDF_Fits = handles.Current_Fit_PDFs;
EM_Fits = handles.Current_Specimen_Fit;%repmat(handles.Current_Fit_Abunds(handles.spec_ind,:), handles.nVar, 1)'.*handles.Current_Fit_EMs;
hold(handles.PDF_Axes, 'on')
plot(handles.PDF_Axes, Xplot, 100.*PDF_Fits, 'LineWidth', 2)
plot(handles.PDF_Axes, Xplot, 100.*EM_Fits, 'LineWidth', 1)
hold(handles.PDF_Axes, 'off')
tmp_r2 = handles.Specimen_QFit(handles.spec_ind,1);
tmp_theta = handles.Specimen_QFit(handles.spec_ind,2);
% Check the specimen name and format correctly
if isnumeric(handles.All_Names{handles.spec_ind})
MSG = [sprintf('%g', handles.All_Names{handles.spec_ind}), '; R^2 = ', sprintf('%2.3f', tmp_r2), ', Theta = ', sprintf('%2.3f', tmp_theta)];
elseif ischar(handles.All_Names{handles.spec_ind})
MSG = [handles.All_Names{handles.spec_ind}, '; R^2 = ', sprintf('%2.3f', tmp_r2), ', Theta = ', sprintf('%2.3f', tmp_theta)];
else
warning('AnalySize:SpecimenName', 'The specimen name may not appear correctly. If not please contact Greig Paterson.');
MSG = [sprintf('%g', handles.All_Names{handles.spec_ind}), '; R^2 = ', sprintf('%2.3f', tmp_r2), ', Theta = ', sprintf('%2.3f', tmp_theta)];
end
set(get(handles.PDF_Axes, 'Title'), 'Interpreter', 'none');
set(get(handles.PDF_Axes, 'Title'), 'String', MSG, 'FontUnits', FUnits, 'FontSize', FontSize2);
% Plot the end members in their axes
cla(handles.EM_Axes, 'reset'); % Reset the axes
set(handles.EM_Axes, 'ColorOrder', handles.Default_Plot_Colors);
hold(handles.EM_Axes, 'on')
plot(handles.EM_Axes, Xplot, 100.*handles.Current_Fit_EMs, 'LineWidth', 1)
hold(handles.EM_Axes, 'off')
set(get(handles.EM_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
set(get(handles.EM_Axes, 'Title'), 'String', 'End Members', 'FontUnits', FUnits, 'FontSize', FontSize2);
set(handles.EM_Axes, 'Box', 'on')
switch Plot_Type
case 1 % Log Scale
set(get(handles.EM_Axes, 'XLabel'), 'String', 'Ln(grain size in \mu{m})', 'FontUnits', FUnits, 'FontSize', FontSize1)
case 2 % Log-Linear Scale
set(handles.EM_Axes, 'XScale', 'Log');
set(get(handles.EM_Axes, 'XLabel'), 'String', 'Grain size [\mu{m}]', 'FontUnits', FUnits, 'FontSize', FontSize1)
case 3 % Phi scale
set(get(handles.EM_Axes, 'XLabel'), 'String', '\phi', 'FontUnits', FUnits, 'FontSize', FontSize1)
case 4 % Linear scale
set(get(handles.EM_Axes, 'XLabel'), 'String', 'Grain size [\mu{m}]', 'FontUnits', FUnits, 'FontSize', FontSize1)
end
else
% Make sure the end member plot is clear
cla(handles.EM_Axes, 'reset'); % Reset the axes
% Set the basic plot
switch Plot_Type
case 1 % Log Scale
set(get(handles.EM_Axes, 'XLabel'), 'String', 'Ln(grain size in \mu{m})', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.EM_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
case 2 % Log-Linear Scale
set(handles.EM_Axes, 'XScale', 'Log');
set(get(handles.EM_Axes, 'XLabel'), 'String', 'Grain size [\mu{m}]', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.EM_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
case 3 % Phi scale
set(get(handles.EM_Axes, 'XLabel'), 'String', '\phi', 'FontUnits', FUnits, 'FontSize', FontSize1)
set(get(handles.EM_Axes, 'YLabel'), 'String', 'Fractional abundance [%]', 'FontUnits', FUnits, 'FontSize', FontSize1);
end
set(get(handles.EM_Axes, 'Title'), 'String', 'End Members', 'FontUnits', FUnits, 'FontSize', FontSize2);
set(handles.EM_Axes, 'Box', 'on')
end
% Reset the button down functions
set(handles.PDF_Axes, 'ButtonDownFcn', {@PDF_Axes_ButtonDownFcn, handles});
set(handles.EM_Axes, 'ButtonDownFcn', {@EM_Axes_ButtonDownFcn, handles});
% --- Executes when selected cell(s) is changed in Data_Table.
function Data_Table_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to Data_Table (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% Get the list of currently selected table cells
sel = eventdata.Indices;
if ~isempty(sel) == 1 % The user is selecting a data row
selrow = sel(1,1);
handles.spec_ind = selrow;
set(handles.Spec_Num, 'String', handles.All_Names{handles.spec_ind}); % set the index
guidata(hObject, handles);
% Readjust table position to keep in frame
try
jscrollpane = javaObjectEDT(findjobj(handles.Data_Table));
viewport = javaObjectEDT(jscrollpane.getViewport);
P = viewport.getViewPosition();
obj_fail = 0; % flag to indicate if findjobj failed or not
catch
% findjobj not avaiable so resort to default behaviour
obj_fail = 1;
end
func_handles=Set_Current_Data(handles);
handles=func_handles;
guidata(hObject, handles);
% Restore the table position
if obj_fail == 0
drawnow()
viewport.setViewPosition(P);
end
end
% --- Executes when selected cell(s) is changed in Fit_Table.
function Fit_Table_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to Fit_Table (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
% Get the list of currently selected table cells
sel = eventdata.Indices;
try any(sel(1,:));
if any(sel(1,:)) == 1 % The user is selecting a data row
selrow = sel(1,1);
handles.Fit_Data_Ind = selrow;
guidata(hObject, handles);
func_handles=Set_Current_Fit(handles);
handles=func_handles;
guidata(hObject, handles);
end
catch
return
end
% --- Executes on button press in Load_Data.
function Load_Data_Callback(hObject, eventdata, handles)
% hObject handle to Load_Data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file, path]=uigetfile({'*.dat;*.$ls;*.mes;*.xls;*.xlsx', 'Supported files (*.dat;*.$ls;*.mes;*.xls;*.xlxs)';...
'*.*', 'All Files (*.*)'},...
'Please select your data file(s).', 'MultiSelect', 'on');
if iscell(file) == 0 & file == 0 %#ok<AND2>
% User cancelled the open dialog box - Do nothing
handles.Data_Loaded = 0;
guidata(hObject, handles);
elseif size(file,2) ~=0 % load and process
% Get the file extension
if iscell(file)
split_name=regexp(file{1,1}, '\.', 'split');
nFiles=size(file,2);
else
split_name=regexp(file, '\.', 'split');
nFiles=1;
end
file_ext=split_name{end};
% Load the different file types
if strcmpi(file_ext, 'xls') || strcmpi(file_ext, 'xlsx')
% Excel file
handles.File_Type_Flag='Excel';
[status,sheets] = xlsfinfo(strcat(path, file));
if strcmpi(file_ext, 'xls') && strcmpi(handles.OS, 'Mac')
warndlg({'Older xls files are not fully supported in OS X.';...
'Please save the file as xlsx or in text format'}, '*.xls not supported')
return
end
Load_Options_XL('Main_Window_Call', handles.AnalySize_MW, sheets);
if ~isempty( getappdata(handles.AnalySize_MW, 'Abort_Cancel') )
% the user canceled or close the window
% remove the flag and simply return, noting no data loaded
rmappdata(handles.AnalySize_MW, 'Abort_Cancel')
handles.Data_Loaded = 0;
guidata(hObject, handles);
return
end
% Retrive the updated values then remove them
type_data = getappdata(handles.AnalySize_MW, 'Type_Data');
guidata(hObject, handles);
rmappdata(handles.AnalySize_MW, 'Type_Data');
elseif strcmpi(file_ext, 'dat') || strcmpi(file_ext, '$ls') || strcmpi(file_ext, 'ls')...
|| strcmpi(file_ext, 'csv') || strcmpi(file_ext, 'mes')
Load_Options('Main_Window_Call', handles.AnalySize_MW);
if ~isempty( getappdata(handles.AnalySize_MW, 'Abort_Cancel') )
% the user canceled or close the window
% remove the flag and simply return, noting no data loaded
rmappdata(handles.AnalySize_MW, 'Abort_Cancel')
return
end
% Retrive the updated values then remove them
handles.File_Delimiter = getappdata(handles.AnalySize_MW, 'File_Delimiter');
handles.File_Type_Flag = getappdata(handles.AnalySize_MW, 'File_Type_Flag');
handles.Multi_Spec_Flag = getappdata(handles.AnalySize_MW, 'Multi_Spec_Flag');
guidata(hObject, handles);
rmappdata(handles.AnalySize_MW, 'File_Delimiter');
rmappdata(handles.AnalySize_MW, 'File_Type_Flag');
rmappdata(handles.AnalySize_MW, 'Multi_Spec_Flag');
type_data={handles.File_Delimiter, handles.Multi_Spec_Flag};
else
% Unsupported file type - Warn and return
MSG = [{'This file type is not currently supported.'}, ...
{'If it is a valid data file and you would like it added please conatct the developer.'}....
{'In the meantime use either *.dat or Excel files'}];
warndlg(MSG, 'Unrecognized file type', 'modal');
% handles.Data_Loaded = 0;
guidata(hObject, handles);
return
end
% Common input to all file formats
file_data=[{handles.File_Type_Flag}, {nFiles}];
[Sample_Names, Grain_Size, Data]=Read_Data_Files(path, file, file_data, type_data);
% Check that all the grain size bins are consistent
unique_lengths = length(unique(cellfun(@length, Grain_Size)));
if unique_lengths ~= 1
warndlg('Number of grain size bins are inconsistent. Please check the data.', 'Data Error!', 'modal');
return;
end
GS = cell2mat(Grain_Size);
LGS = log(GS);
% Check unique grain size bins and try rounding to obtain consistent bins
if length(unique(GS)) ~= size(GS, 1)
% The length is inconsistent
% Try rounding
nGS = round(GS.*1e3)./1e3;
if length(unique(nGS)) == size(GS, 1)
% Sizes are now consistent, but tell the user
warndlg('Grain size bins have been rounded to 3 d.p. for consisitency.', 'Rounded Grain Sizes', 'modal');
else
% The length is still inconsistent
% Try rounding in logspace
nGS = exp(round(LGS.*1e2)./1e2);
if length(unique(nGS)) == size(GS, 1)
warndlg('Grain size bins have been rounded to 2 d.p. in log space for consisitency.', 'Rounded Grain Sizes', 'modal');
else
% Still inconsistent
% Throw a warning and do not open the data
warndlg('Grain size bins are inconsistent. Please check the data.', 'Data Error!', 'modal');
return;
end
end
% Assign the new GS to GS
GS = nGS;
Grain_Size = mat2cell(GS, size(GS,1), ones(1,nFiles)); % update the grain size cell
end
% Check and remove zeros from the grain sizes
inds = (GS(:,1) == 0);
Data = cellfun(@(x) x(~inds), Data, 'UniformOutput', 0);
Grain_Size = cellfun(@(x) x(~inds), Grain_Size, 'UniformOutput', 0);
% Set the data to the handles
handles.All_Names=Sample_Names;
handles.All_Data=Data;
handles.All_GS=Grain_Size;
handles.All_LGS=cellfun(@log, Grain_Size, 'UniformOutput', 0);
handles.All_Phi = cellfun(@(x) -log2(x./1e3), handles.All_GS, 'Uniformoutput', 0);
handles.spec_ind=1; % Set the specimen index to 1
handles.nVar=length(Data{1}); % Get the number of variables
handles.Nspec=length(Data); % Get the number of specimens
set(handles.Spec_Num, 'String', handles.All_Names{handles.spec_ind}); % set the index
% Reset the fits and tables
func_handles = SetDefaultHandles(handles, 'All');
handles = func_handles;
handles.Data_Loaded = 1;
% Set and plot the current data
func_handles=Set_Current_Data(handles);
handles=func_handles;
% Set the data table info
set(handles.Data_Table, 'Data', handles.All_Names, 'ColumnName', handles.Table_Cols);
% save the updated handles
guidata(hObject, handles);
end
% --- Executes on button press in Load_Test_Data.
function Load_Test_Data_Callback(hObject, eventdata, handles)
% hObject handle to Load_Test_Data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% THIS IS NOT A PRIMARY FUNCTION AND IS DESIGNED FOR TESTING ONLY
% THIS SHOULD NOT BE ENABLED
% choice = menu('Select Test Data', 'Synthetic 1 (50x80, 3 EMs)', 'Real 1 (188x100)', 'Real 2 (1138x100)',...
% 'Real 3 (157x116)', 'Real 4 (151x56)', 'Toy 1 (49x80, 2 EMs)', 'Toy 2 (99x80, 2 EMs)', ...
% 'Weltje I (200x45)', 'Weltje II (200x45)', 'Weltje III (200x45)');
%
%
% S = mfilename('fullpath');
% name_len = length(mfilename());
% MyPath = S(1:end-name_len);
%
%
% switch choice
% case 1
% load(strcat(MyPath, 'Test_Data/Synth_Data.mat'));
% case 2
% load(strcat(MyPath, 'Test_Data/Data_Trimmed.mat'));
% case 3
% load(strcat(MyPath, 'Test_Data/Data.mat'));
% case 4
% load(strcat(MyPath, 'Test_Data/Real3.mat'));
% case 5
% load(strcat(MyPath, 'Test_Data/Real4.mat'));
% case 6
% load(strcat(MyPath, 'Test_Data/Toy1.mat'));
% case 7
% load(strcat(MyPath, 'Test_Data/Toy2.mat'));
% case 8
% load(strcat(MyPath, 'Test_Data/Weltje_I.mat'));
% case 9
% load(strcat(MyPath, 'Test_Data/Weltje_II.mat'));
% case 10
% load(strcat(MyPath, 'Test_Data/Weltje_III.mat'));
%
% otherwise
% % Do nothing
% return
% end
%
% % Normalize the data
% Data=bsxfun(@rdivide, Data,sum(Data, 2));
%
% handles.spec_ind=1; % Set the specimen index to 1
% handles.Nspec=size(Data, 1); % Get the number of specimens
% handles.nVar=size(Data, 2); % Get the number of variables
%
% % Reset the fits and tables
% func_handles = SetDefaultHandles(handles, 'All');
% handles = func_handles;
%
% handles.Data_Loaded = 1;
%
% handles.All_Names = cellstr(strcat('Spec', num2str((1:handles.Nspec)')));
% handles.All_Data = num2cell(Data,2);
% handles.All_GS = num2cell(repmat(GS, 1, handles.Nspec), 1);
% handles.All_LGS = num2cell(repmat(log(GS), 1, handles.Nspec), 1);
% handles.All_Phi = cellfun(@(x) -log2(x./1e3), handles.All_GS, 'Uniformoutput', 0);
%
% set(handles.Spec_Num, 'String', handles.All_Names{handles.spec_ind}); % set the index
%
% % Set the data table info
% set(handles.Data_Table, 'Data', handles.All_Names, 'ColumnName', handles.Table_Cols);
%
% % Set and plot the current data
% func_handles=Set_Current_Data(handles);
% handles=func_handles;
%
% % save the updated handles
guidata(hObject, handles);
% --- Executes on button press in either Next_Spec or Prev_Spec.
function Prev_Next_Spec_Callback(hObject, eventdata, handles, str)
% hObject handle to Next_Sample (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
% Get the index pointer and the addresses
index = handles.spec_ind;
Nspec = handles.Nspec;
catch
return;
end
% Depending on whether Prev or Next was clicked change the display
switch str
case 'Prev'
% Decrease the index by one
ind = index - 1;
% If the index is less then one then set it the number of specimens
% (Nspec)
if ind < 1
ind = Nspec;
end
case 'Next'
% Increase the index by one
ind = index + 1;
% If the index is greater than the snumber of specimens set index
% to 1
if ind > Nspec
ind = 1;
end
end
handles.spec_ind=ind;
set(handles.Spec_Num, 'String', handles.All_Names{handles.spec_ind});
guidata(hObject,handles);
func_handles=Set_Current_Data(handles);
handles=func_handles;
guidata(hObject,handles);
% --- Executes on selection change in Plot_Type.
function Plot_Type_Callback(hObject, eventdata, handles)
% hObject handle to Plot_Type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns Plot_Type contents as cell array
% contents{get(hObject,'Value')} returns selected item from Plot_Type
try
handles.All_Data;
catch
warndlg('No data currently loaded.', 'No Data', 'modal')
return;
end
Update_Plots(handles);
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function Plot_Type_CreateFcn(hObject, eventdata, handles)
% hObject handle to Plot_Type (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Do_EMA.
function Do_EMA_Callback(hObject, eventdata, handles)
% hObject handle to Do_EMA (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
% Send the data to the maui GUI figure
setappdata(handles.AnalySize_MW, 'Data_To_Fit', cell2mat(handles.All_Data) );
setappdata(handles.AnalySize_MW, 'Size', handles.All_GS{1});
catch
warndlg('No data currently loaded.', 'No Data', 'modal')
return
end
% Check that a bare minimum of 10 data are loaded - this is a bare minimum!
if handles.Nspec < 10
MSG = [{'Not enough data are currently loaded.'},...
{'At least 10 specimens are needed to try EMA, but 30 or more are recommended for robust results'}];
warndlg(MSG, 'Not Enough Data', 'modal')
return
end
% Load the fit options
Fit_Opt_Return = Fit_Options('Main_Window_Call', handles.AnalySize_MW, handles.Sel_EM_Data);
if Fit_Opt_Return.FitStatus == 1
% Set the handle fit status
handles.FitStatus = Fit_Opt_Return.FitStatus;
handles.Plot_Fits_Flag = 1; % Set the plot fit flag
% Process the fit data
handles.All_Fit_EMs = [handles.All_Fit_EMs, {Fit_Opt_Return.EndMembers}];
handles.All_Fit_Abunds = [handles.All_Fit_Abunds, {Fit_Opt_Return.Abundances}];
handles.All_Fit_N = handles.All_Fit_N + 1; % Add 1 to the counter for the number of saved fits
handles.Fit_Data_Ind = handles.All_Fit_N;% set the fit data index to move to the current fit
handles.All_DataSet_QFit = [handles.All_DataSet_QFit, Fit_Opt_Return.Fit_Quality(1)];
handles.All_Specimen_QFit = [handles.All_Specimen_QFit, Fit_Opt_Return.Fit_Quality(2)];
handles.All_Fit_Types = [handles.All_Fit_Types, {char(Fit_Opt_Return.Fit_Type)}];
if isfield(Fit_Opt_Return, 'Dist_Params')
handles.All_Fit_Params = [handles.All_Fit_Params, {Fit_Opt_Return.Dist_Params}];
% Save the data for the select EM plots
switch char(Fit_Opt_Return.Fit_Type)
case 'Lognormal'
ind = 1;
case 'Gen. Weibull'
ind = 2;
case 'Weibull'
ind = 3;
case 'SGG'
ind = 4;
case 'GEV'
ind = 5;
end
OW_Flag = 1; % A flag to determine whether to overwrit or not
if isstruct(handles.Sel_EM_Data{ind})
% Some data already exist
Fit_Type = char(Fit_Opt_Return.Fit_Type);
Old_Data = handles.Sel_EM_Data{ind};
New_Data = Fit_Opt_Return.SelectEM_Data;
EM1o = Old_Data.EM_Min;
EM2o = Old_Data.EM_Max;
EM1n = New_Data.EM_Min;
EM2n = New_Data.EM_Max;
MSG = [ {['Variance for ', Fit_Type, ' fits with ', num2str(EM1o), ' to ', num2str(EM2o), ' end members is already saved.']},...
{['The new ', Fit_Type, ' data haves ', num2str(EM1n), ' to ', num2str(EM2n), ' end members.']},...
{'Do you wish to keep the existing data, overwrite it, or append the data sets (duplicates will be overwritten)?'} ];
choice = questdlg(MSG, 'Overwrite end member variance data?', 'Keep Exisiting', 'Overwrite', 'Append', 'Append');
switch choice
case 'Append'
OW_Flag = 2;
case 'Keep Exisiting'
OW_Flag = 3;
end
end
if OW_Flag == 1 % Overwrite
handles.Sel_EM_Data(ind) = {Fit_Opt_Return.SelectEM_Data};
elseif OW_Flag == 2 % Append with overwriting duplicates
handles.Sel_EM_Data(ind) = {Append_EM_Vars(handles.Sel_EM_Data{ind}, Fit_Opt_Return.SelectEM_Data)};
end
% else do nothing and keep the exisiting data
else
handles.All_Fit_Params = [handles.All_Fit_Params, {[]}];
end
% Set the current fit data
func_handles = Set_Current_Fit(handles);
handles = func_handles;
% Get the 95th percentiles for R^2 and theta
nData = handles.Nspec;
R2_95 = GetPercentile([0,diff(0:nData-1)], sort(handles.Specimen_QFit(:,1), 'descend')', 95);
Theta_95 = GetPercentile([0,diff(0:nData-1)], sort(handles.Specimen_QFit(:,2), 'descend')', 5);
% Set the table data
handles.Fit_Table_Data = [handles.Fit_Table_Data;...
{handles.Current_Fit_Type}, {handles.nEnd}, {handles.DataSet_QFit(3)}, {handles.DataSet_QFit(1)}, {handles.DataSet_QFit(2)},...
{R2_95}, {Theta_95}];
set(handles.Fit_Table, 'Data', handles.Fit_Table_Data);
guidata(hObject, handles);
end % else do nothing (keep the old plots and fit)
guidata(hObject, handles);
% --- Executes on button press in Save_Data_Plot.
function Save_Data_Plot_Callback(hObject, eventdata, handles)
% hObject handle to Save_Data_Plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
handles.All_Data;
catch
warndlg('No data currently loaded.', 'No Data', 'modal')
return;
end
[file,path] = uiputfile(strcat(handles.All_Names{handles.spec_ind}, '.eps'),'Save the specimen plot...');
if ~ischar(file) && file==0
% User has cancelled
% Do nothing and...
return;
end
tmpFig=figure('Visible', 'off', 'Units', 'Centimeters','PaperPositionMode','auto');
oldPos=get(tmpFig, 'Position');
set(tmpFig, 'Position', [oldPos(1), oldPos(2), 9, 7.5]); % make the figure bigger than needed (300x300)
% Copy and adjust the axes
newAxes=copyobj(handles.PDF_Axes, tmpFig);