-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnt_dataview.m
executable file
·365 lines (309 loc) · 10.4 KB
/
nt_dataview.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
function [p,data]=nt_dataview(data,p)
%[p,data]=nt_dataview(data,p) - view data sets
%
%
% DATA: matrix, struct, file or directory to view
% P: parameter structure
%
%
VERBOSE=1;
if nargin < 1;
% look for file in entire machine
[p,data]=nt_dataview('/');
return
end
if nargin < 2; p=[]; end
if isempty(data); return; end
% % update string to recreate this view
% if ~isfield(p,'recreate');
% % first call, call string depends on number of arguments
% if nargout==2;
% s1='[p,data]=';
% elseif nargout==1;
% s1='[p]=';
% else
% s1='';
% end
% if isa(data,'char')
% s2=data;
% else
% s2=inputname(1);
% end
% if nargout==2;
% s3=',p);';
% else
% s3=');';
% end
% p.recreate=[s1,'nt_dataview(',s2,s3];
% else
% % append new call
% p.recreate=([p.recreate,'; [p,data]=nt_data_view(data,p);']);
% end
% name to display on dialog or window
if ~isfield(p,'data_name')
if isa(data,'char');
p.data_name=data;
elseif isa(data,'struct');
p.data_name='structure';
elseif isnumeric(data) %isa(data,'double');
p.data_name='matrix';
else
error('argument should be string, struct, or numeric');
end
end
%%%%%%%%%%%%%%%%%%%%%%%% MATRIX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isnumeric(data)
if ~isa(data, 'double')
warning('converting data to double');
data=double(data);
end
if ~isempty(p) && isfield(p, 'matrix_modify');
eval([p.matrix_modify,';']); % modify data
end
nt_whoss;
if VERBOSE; disp('matrix'); end
nDims=ndims(data);
if nDims==2 && (size(data,1)==1 || size(data,2)==1); nDims=1; end
if nDims>4; nDims=4; end
% positions
posFig=[0 100, 1000, 400];
posButtonReturn=[50, 20, 100, 25];
posButtonAssign=[200, 20, 300, 25];
posEdit=[100 70 800 30];
% put up window
dialogH=figure('Visible','on', 'WindowStyle','normal', 'Name',p.data_name, 'NumberTitle','off',...
'Pointer','arrow', 'Position',posFig,'color', [1 1 1], 'KeyPressFcn',@doFigureKeyPress,...
'IntegerHandle','off', 'CloseRequestFcn' ,@doDelete);
editH=[]; buttonReturnH=[];
set(gcf,'userdata',p);
done=0;
while ~done
% plot summary statistics for all dimensions
plot_params.bottom=0.35; plot_params.height=0.5;
%%%%%%%%%%%%%%%%%% HERE's where we plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nt_statmatrix(data,plot_params);
if isfield(p,'return') && p.return; break; end
%set(dialogH,'HandleVisibility','callback');
whichButton=[];
returnPressed=[];
escapePressed=[];
otherKeyPressed=[];
% create return button
buttonReturnH=uicontrol('style', 'pushbutton', 'string', 'Return','position', posButtonReturn,...
'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
'fontSize', 14);
% create return button
buttonAssignH=uicontrol('style', 'pushbutton', 'string', 'Assign to p,data in workspace','position', posButtonAssign,...
'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
'fontSize', 14);
editString=['data=data( 1 : ',num2str(size(data,1))];
for iDim=2:nDims
editString=[editString,', 1 : ',num2str(size(data,iDim))]; % full index for that dimension
end
editString=[editString,' );'];
editH=uicontrol('Style','edit','String',editString,'position', posEdit, 'parent', dialogH,...
'callback', @editCallback, 'foregroundcolor',[1 1 1]*0 );
% wait for user input
if ~ ishghandle(dialogH); return; end
uiwait(dialogH);
if ~ ishghandle(dialogH); return; end
% act on user input
if returnPressed % keyboard
done=1;
elseif escapePressed % keyboard
done=1;
elseif otherKeyPressed % keyboard
;
else
h=get(dialogH,'CurrentObject');
if find(h==editH) % one of the edit boxes
s=get(editH(1),'string');
try
th=annotation('textbox', [.5 .04 .1 .1], 'string', 'evaluating...', 'fontsize', 14, 'edgecolor', [1 1 1]);
drawnow
if ~isempty(p) && isfield(p, 'matrix_modify');
p.matrix_modify=[p.matrix_modify, s];
else
p.matrix_modify=s;
end
eval(s);
set(th,'string','');
clf
catch
beep;
warning(['incorrect indexing string: >',s,'<']);
end
elseif h==buttonReturnH
done=1;
elseif h==buttonAssignH
assignin('base','p',p);
assignin('base','data',data);
done=1;
else
disp(num2str(h));
error('unexpected handle')
end
end
%clf
end
set(gcf,'userdata',p);
if ~isempty(editH); delete(editH); end
if ~isempty(buttonReturnH); delete(buttonReturnH); end
if ishghandle(dialogH); delete(dialogH); end
%set(buttonH, 'string', 'Recreate','Callback',@doButtonPress2);
% return data - or not
if nargout==0; clear data p; end
if VERBOSE; disp('returning from nt_dataview...'); end
%return;
%%%%%%%%%%%%%%%%%%%%%%%% STRUCT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif isa(data, 'struct')
% struct in workspace
if VERBOSE; disp('struct'); end
if isempty(struct); error('''struct'' is empty!'); end
field_names=fieldnames(data);
if isempty(field_names); error('structure is empty!'); end
field_sizes=zeros(numel(field_names),1);
for k=1:numel(field_names);
field=getfield(data,field_names{k});
field_sizes(k)=round(prod(size(field))/1024);
end
clear field;
a=repmat(' (', numel(field_names),1);
b=cellstr(num2str(field_sizes, '%9d'));
b=char(b);
c=[repmat(' Mb)', numel(field_names),1)];
i=listdlg('liststring',cellstr([char(field_names),a,b,c]),...
'name', 'Select field in struct:', ...
'listsize', [600 300], ...
'OKstring','Select',...
'PromptString',p.data_name);
% call this function on the selected field
if i
data=getfield(data,field_names{i});
[p,data]=nt_dataview(data,p);
if nargout==0; data=[]; end
return
end
elseif isa(data, 'char') && ...
( exist(data,'file')==2 || ...
(numel(data)>3 && all(data(end-2:end)=='.ds'))) % treat as file
%%%%%%%%%%%%%%%%%%%%%%%% FILE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fname=data;
if VERBOSE; disp('file'); end
if 0
% intercept mat files
if numel(fname)>4 & fname(end-3:end)=='.mat'
if VERBOSE; disp('mat file'); end
S=whos('-file',fname);
var_names=char(S.name);
var_sizes=round([S.bytes]/1024)';
a=repmat(' (', size(var_names,1),1);
b=cellstr(num2str(var_sizes, '%9d'));
b=char(b);
c=[repmat(' Mb)', size(var_names,1),1)];
i=listdlg('liststring',cellstr([var_names,a,b,c]),...
'name', 'Select variable in file:', ...
'listsize', [600 300], ...
'OKstring','Select',...
'PromptString',p.data_name);
if i
X=load(fname,var_names(i,:));
[p,data]=nt_dataview(X,p);
end
if nargout==0; data=[]; end
return
end
end
% hand over to data file reader
[p,data]=nt_read_data(fname);
[p,data]=nt_dataview(data,p);
return
%%%%%%%%%%%%%%%%%%%%%%%% DIRECTORY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif isa(data, 'char') && exist(data, 'dir')==7
% directory
if VERBOSE; disp('directory'); end
d=dir(data);
if numel(d)==0
error(['directory >',data,'< not found']);
end
fnames=char(d.name);
idx=find(fnames(:,1)~='.'); % remove '.' and '..' and invisible files
d=d(idx);
fnames=fnames(idx,:);
% separate directories and files
didx=find([d.isdir]);
fidx=find(~[d.isdir]);
fnames=fnames([didx, fidx],:);
% count files within the directories
nfiles=zeros(numel(didx),1);
for k=1:numel(didx)
dd=dir([data,'/',d(didx(k)).name]);
fns=char(dd.name);
idx=find(fns(:,1)~='.'); % remove '.' and '..' and invisible files
nfiles(k)=numel(idx);
end
% size of the files
mbytes=[d(fidx).bytes]'/1024;
% string arrays to put in dialog list
a=repmat(' (', numel(d),1);
if numel(didx)>0
b=cellstr(num2str(nfiles, '%9d'));
else
b=[]; % stupid matlab!
end
if numel(fidx)>0
b=[b;cellstr(num2str(mbytes,'%0.1f'))];
end
b=char(b);
c=[repmat(' files)', numel(didx),1); repmat(' Mb) ', numel(fidx),1)];
% which directory or file is user interested in?
i=listdlg('liststring',cellstr([fnames,a,b,c]),...
'name', 'Select file:', ...
'listsize', [300 300], ...
'OKstring','Select',...
'PromptString',p.data_name);
% call this function on that file
if i
data=strcat(data,'/',fnames(i,:));
[p,data]=nt_dataview(data,p);
end
return
else
disp([p.data_name,' not found']);
if nargout==0; data=[]; end
return
end
%h=data;
if nargout==0;
disp('hereiam');
clear data p
end
%%%%%%%%%%%%%%%%%%%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function doFigureKeyPress(obj, evd) %#ok
switch(evd.Key)
case {'return','space'}
returnPressed = true;
case 'escape'
escapePressed=true;
otherwise
otherKeyPressed=true;
end
uiresume(gcbf)
end
function doDelete(varargin)
delete(dialogH);
end
function editCallback(obj,evd);
%editString = get(obj,'String');
uiresume(gcbf);
end
function doButtonPress(obj,evd);
whichButton=obj;
uiresume(gcbf);
end
function doButtonPress2(obj,evd);
whichButton=obj;
p=get(gcf,'userdata');
evalin('caller',p.recreate);
end
end % this file's main function