-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPSFileSelector.m
More file actions
83 lines (49 loc) · 2.69 KB
/
XPSFileSelector.m
File metadata and controls
83 lines (49 loc) · 2.69 KB
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
function [ DataArray , OutputHeaders , DataName ] = XPSFileSelector ( )
%% Opens user interface to select XPS peak report files generated by CasaXPS to import for analysis.
% Could also be used to import any multi-column set of spectra .txt data.
[ FileGroup , DataPath ] = uigetfile ( '*.txt' , 'DialogTitle' , 'Select XPS .txt files' , 'MultiSelect' , 'on' ) ; % Gets file names and location.
DataName = FileGroup { 1 } ;
DataName ( end - 7 : end ) = [] ;
SingleFile = double ( ischar ( FileGroup ) ) ; % Used to account for the case where only a single file is selected.
if SingleFile > 0
NFC = 1 ;
else
NFC = length ( FileGroup ) ; % Number of files to import.
end
FileSet = repmat ( { '' } , 1 , NFC ) ; % Generates cell array to place filenames into.
cd ( DataPath ) ; % Sets target directory to current folder.
%% Generates array of files for analysis.
for i = 1 : NFC
if SingleFile > 0
FileSet ( i ) = cellstr ( strcat ( DataPath , FileGroup ) ) ; % Concatenates path and file strings and adds to output array,
else
FileSet ( i ) = strcat ( DataPath , FileGroup ( i ) ) ; % Concatenates path and file strings and adds to output array,
end
end
%% Uses column headers so user can choose which data to extract and process.
Delimiter = '\t' ; % Could be specified in textscan to avoid variable creation.
NFile = fopen ( char ( FileSet ( : , 1 ) ) , 'r' ) ; % Chosen file to determine number of columns, set to 1 to use first specified file.
StartRow = 4 ; % First row of data. Might need to be changed for different file types/sources.
for i = 1 : StartRow - 1
Headers = fgetl ( NFile ) ;
end
Headers = textscan ( Headers , '%s' , 'Delimiter' , Delimiter ) ;
NF = numel ( Headers { 1 } ) ;
[ DataChoices ] = listdlg ( 'ListString' , Headers { 1 } ) ;
N = length ( DataChoices ) ; % Specifies number of columns of data.
FormatSpec = repmat ( '%n' , 1 , NF ) ; % Specifies import format;
DataArray = repmat ( { '' } , 1 , N , NFC ) ; % Generates cell array to place data into.
OutputHeaders = repmat ( { '' } , 1 , N ) ;
%% Opens, reads and closes data files.
for i = 1 : NFC
% Chooses file.
FileChoice = char ( FileSet ( : , i ) ) ;
FileID = fopen ( FileChoice , 'r' ) ;
FileArray = textscan ( FileID , FormatSpec , 'Delimiter' , Delimiter , 'EmptyValue' , NaN , 'HeaderLines' , StartRow - 1 , 'ReturnOnError' , false ) ;
fclose ( FileID ) ;
for k = 1 : N
% Places array data into pre-defined recipient array.
DataArray { 1 , k , i } = FileArray { DataChoices ( k ) } ;
OutputHeaders{ k } = char ( Headers { 1 } ( DataChoices ( k ) ) ) ;
end
end