This repository was archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_export.m
More file actions
163 lines (133 loc) · 5.03 KB
/
callback_export.m
File metadata and controls
163 lines (133 loc) · 5.03 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
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
function callback_export(hObject,eventdata,type)
% CALLBACK_EXPORT allows user to export data to *.txt or *.xlsx files
%__________________________________________________________________________
% USAGE: callback_export(hObject,eventdata,type)
%
% INPUT: hObject - calling object handle
% eventdata - not used, MATLAB required
% type = 'all' or 'selected'
%__________________________________________________________________________
try
% 1 - COLLECT INFORMATION FROM GUI
GUI = guidata(hObject);
h = guihandles(hObject);
% 2 - COLLECT DATA FROM GUI
switch type
% 2.1 - Collect all the data from
case 'all';
% 2.1.1 - Determine the selected stations
use = getselected(hObject);
if isempty(use);
errordlg('You must select a station','ERROR'); return;
end
% 2.1.2 - Extract data from selected stations
for i = 1:length(use);
[DATA{i},HEAD{i}] = getalldata(h.(use{i}));
end
% 2.2 - Collect selected data from selected variables
case 'selected'
% 2.2.1 - Check the variable window is open
if isempty(GUI.varwindow) || ~ishandle(GUI.varwindow)
errordlg('You must select variables.','ERROR');
return;
end
% 2.2.2 - Extract selected data
[DATA,HEAD,use] = getselecteddata(GUI.primary);
end
% 3 - CROP DATA BETWEEN TIMES IF DESIRED
mes = ['Would you like to crop the data between the selected ',...
'date/times or write the entire data set?'];
q = questdlg(mes,'Crop Data','Crop','Entire','Crop');
if strcmpi(q,'Crop');
[DATA] = cropdata(DATA,GUI.time);
end
% 4 - WRITE DATA TO FILE(S)
% 4.1 - Get filenames and type
FilterSpec = {'*.xls','Excel 97-03 (*.xls)';...
'*.xlsx','Excel 2007 (*.xlsx)';'*.txt','Text (*.txt)'};
[fname,fpath,fidx] = uiputfile(FilterSpec,'Save file(s) as...');
if fname == 0; return; end
% 4.2 - Write file(s)
hbar = waitbar(0,'Writing data to file, please wait...');
for i = 1:length(DATA);
waitbar(i/length(DATA),hbar);
writefile(HEAD{i},DATA{i},use{i},[fpath,fname]);
end
close(hbar);
catch
mes = ['An error occured exporting the data (callback_export), ',...
'see errorlog.txt'];
errorlog(mes);
end
%--------------------------------------------------------------------------
% SUBFUNCTION: writefile
function [out] = writefile(head,data,station,filename)
% 1 - Seperate desired filename into parts
[pth,file,ext] = fileparts(filename);
DATA = num2cell(data,2);
% 2 - Case when a comma deliminated text file is desired
switch ext
case '.txt'
% 2.1 - Build current filename
filename = [pth,'\',file,'_',station,'.txt'];
[r,c] = size(data);
% 2.2 - Build a format text string
frm = '';
for i = 2:c; frm = [frm,',%f']; end; frm = [frm,'\n'];
% 2.3 - Write data to file
fid = fopen(filename,'w');
for i = 1:r;
d = DATA{i}; % Numeric Data
t = datestr(d(1),'mm-dd-yy HH:MM'); % Date/time
fprintf(fid,'%s',t); % Write date/time
fprintf(fid,frm,d(2:c)); % Write numeric
end
fclose(fid);
% 3 - Case shen excel file is desired
case {'.xlsx','.xls'}
xlswrite(filename,head,station);
xlswrite(filename,data,station,'A2')
end
%--------------------------------------------------------------------------
% SUBFUNCTION: cropdata
function [out] = cropdata(in,t)
% CROPDATA removes data between the specified date/times
for i = 1:length(in);
data = in{i};
time = data(:,1);
idx = time >= t(1) & time <= t(2);
out{i} = data(idx,:);
end
%--------------------------------------------------------------------------
% SUBFUNCTION: getselecteddata
function [DATA,head,sta] = getselecteddata(handle)
% GETSELECTEDDATA extracts all of the data from the selected station
% Get handle information from primay axis
user = get(handle,'UserData');
station = user.sta_panel;
% Loop through each station and build output arrays for each
for i = 1:length(station);
U = get(station(i),'UserData');
[C,head{i}] = get_data(station(i));
head{i} = ['Time',head{i}];
sta{i} = [user.sta_name{i}];
Y = [];
for j = 2:2:length(C); Y = [Y,C{j}]; end
DATA{i} = [U.Time,Y];
end
%--------------------------------------------------------------------------
% SUBFUNCTION: getalldata
function [DATA,head] = getalldata(handle)
% GETALLDATA extracts all of the data from the selected station
% Find variable fieldnames
user = get(handle,'UserData');
V = user.variables;
fn = fieldnames(V);
% Loop through each variable and build output arrays
DATA = user.Time; head = {'Time'};
for i = 1:length(fn);
label = V.(fn{i}).label;
unit = V.(fn{i}).unit;
DATA = [DATA,V.(fn{i}).data];
head = [head,[label,' (',unit,')']];
end