Home > NoiseTools > nt_dataview.m

# nt_dataview

## PURPOSE

[p,data]=nt_dataview(data,p) - view data sets

## SYNOPSIS

function [p,data]=nt_dataview(data,p)

## DESCRIPTION

    [p,data]=nt_dataview(data,p) - view data sets

      DATA: matrix, struct, file or directory to view
      P: parameter structure

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_dataview [p,data]=nt_dataview(data,p) - view data sets
- nt_read_data [p,data]=nt_read_data(fname,flag) - read data from file
- nt_statmatrix stats=nt_statMatrix(x,plot_params) - calculate statistics arrays for each dim of matrix
- nt_whoss size=nt_whoss - total Gbytes used by variables

This function is called by:

- nt_dataview [p,data]=nt_dataview(data,p) - view data sets

## SUBFUNCTIONS

- function doFigureKeyPress(obj, evd)
- function doDelete(varargin)
- function editCallback(obj,evd);
- function doButtonPress(obj,evd);
- function doButtonPress2(obj,evd);

## SOURCE CODE

    0001 function [p,data]=nt_dataview(data,p)
    0002 %[p,data]=nt_dataview(data,p) - view data sets
    0003 %
    0004 %
    0005 %  DATA: matrix, struct, file or directory to view
    0006 %  P: parameter structure
    0007 %
    0008 %
    0009 VERBOSE=1;
    0010
    0011 if nargin < 1;
    0012     % look for file in entire machine
    0013     [p,data]=nt_dataview('/');
    0014     return
    0015 end
    0016
    0017 if nargin < 2; p=[]; end
    0018 if isempty(data); return; end
    0019
    0020 % % update string to recreate this view
    0021 % if ~isfield(p,'recreate');
    0022 %     % first call, call string depends on number of arguments
    0023 %     if nargout==2;
    0024 %         s1='[p,data]=';
    0025 %     elseif nargout==1;
    0026 %         s1='[p]=';
    0027 %     else
    0028 %         s1='';
    0029 %     end
    0030 %     if isa(data,'char')
    0031 %         s2=data;
    0032 %     else
    0033 %         s2=inputname(1);
    0034 %     end
    0035 %     if nargout==2;
    0036 %         s3=',p);';
    0037 %     else
    0038 %         s3=');';
    0039 %     end
    0040 %     p.recreate=[s1,'nt_dataview(',s2,s3];
    0041 % else
    0042 %     % append new call
    0043 %     p.recreate=([p.recreate,'; [p,data]=nt_data_view(data,p);']);
    0044 % end
    0045
    0046
    0047 % name to display on dialog or window
    0048 if ~isfield(p,'data_name')
    0049     if isa(data,'char');
    0050         p.data_name=data;
    0051     elseif isa(data,'struct');
    0052         p.data_name='structure';
    0053     elseif isnumeric(data) %isa(data,'double');
    0054         p.data_name='matrix';
    0055     else
    0056         error('argument should be string, struct, or numeric');
    0057     end
    0058 end
    0059
    0060
    0061 %%%%%%%%%%%%%%%%%%%%%%%%   MATRIX  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0062 if isnumeric(data)
    0063
    0064     if ~isa(data, 'double')
    0065         warning('converting data to double');
    0066         data=double(data);
    0067     end
    0068
    0069     if ~isempty(p) && isfield(p, 'matrix_modify');
    0070         eval([p.matrix_modify,';']); % modify data
    0071     end
    0072
    0073     nt_whoss;
    0074     if VERBOSE; disp('matrix'); end
    0075     nDims=ndims(data);
    0076     if nDims==2 && (size(data,1)==1 || size(data,2)==1); nDims=1; end
    0077     if nDims>4; nDims=4; end
    0078
    0079     % positions
    0080     posFig=[0 100, 1000, 400];
    0081     posButtonReturn=[50, 20, 100, 25];
    0082     posButtonAssign=[200, 20, 300, 25];
    0083     posEdit=[100 70 800 30];
    0084
    0085     % put up window
    0086     dialogH=figure('Visible','on', 'WindowStyle','normal', 'Name',p.data_name, 'NumberTitle','off',...
    0087         'Pointer','arrow', 'Position',posFig,'color', [1 1 1], 'KeyPressFcn',@doFigureKeyPress,...
    0088         'IntegerHandle','off', 'CloseRequestFcn' ,@doDelete);
    0089     editH=[]; buttonReturnH=[];
    0090     set(gcf,'userdata',p);
    0091     done=0;
    0092     while ~done
    0093
    0094         % plot summary statistics for all dimensions
    0095         plot_params.bottom=0.35; plot_params.height=0.5;
    0096
    0097         %%%%%%%%%%%%%%%%%%  HERE's where we plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0098         nt_statmatrix(data,plot_params);
    0099
    0100         if isfield(p,'return') && p.return; break; end
    0101
    0102         %set(dialogH,'HandleVisibility','callback');
    0103         whichButton=[];
    0104         returnPressed=[];
    0105         escapePressed=[];
    0106         otherKeyPressed=[];
    0107
    0108         % create return button
    0109         buttonReturnH=uicontrol('style', 'pushbutton', 'string', 'Return','position', posButtonReturn,...
    0110             'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
    0111             'fontSize', 14);
    0112
    0113         % create return button
    0114         buttonAssignH=uicontrol('style', 'pushbutton', 'string', 'Assign to p,data in workspace','position', posButtonAssign,...
    0115             'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
    0116             'fontSize', 14);
    0117
    0118         editString=['data=data( 1 : ',num2str(size(data,1))];
    0119         for iDim=2:nDims
    0120             editString=[editString,', 1 : ',num2str(size(data,iDim))]; % full index for that dimension
    0121         end
    0122         editString=[editString,' );'];
    0123         editH=uicontrol('Style','edit','String',editString,'position', posEdit, 'parent', dialogH,...
    0124             'callback', @editCallback, 'foregroundcolor',[1 1 1]*0 );
    0125         % wait for user input
    0126         if ~ ishghandle(dialogH); return; end
    0127         uiwait(dialogH);
    0128         if ~ ishghandle(dialogH); return; end
    0129
    0130         % act on user input
    0131         if returnPressed % keyboard
    0132             done=1;
    0133         elseif escapePressed % keyboard
    0134             done=1;
    0135         elseif otherKeyPressed % keyboard
    0136             ;
    0137         else
    0138             h=get(dialogH,'CurrentObject');
    0139
    0140             if find(h==editH) % one of the edit boxes
    0141                 s=get(editH(1),'string');
    0142                 try
    0143                    th=annotation('textbox', [.5 .04 .1 .1], 'string', 'evaluating...', 'fontsize', 14, 'edgecolor', [1 1 1]);
    0144                    drawnow
    0145                    if ~isempty(p) && isfield(p, 'matrix_modify');
    0146                        p.matrix_modify=[p.matrix_modify, s];
    0147                    else
    0148                        p.matrix_modify=s;
    0149                    end
    0150                    eval(s);
    0151                    set(th,'string','');
    0152                    clf
    0153                 catch
    0154                    beep;
    0155                    warning(['incorrect indexing string: >',s,'<']);
    0156                 end
    0157             elseif h==buttonReturnH
    0158                 done=1;
    0159             elseif h==buttonAssignH
    0160                 assignin('base','p',p);
    0161                 assignin('base','data',data);
    0162                 done=1;
    0163             else
    0164                 disp(num2str(h));
    0165                 error('unexpected handle')
    0166             end
    0167         end
    0168         %clf
    0169     end
    0170
    0171     set(gcf,'userdata',p);
    0172     if ~isempty(editH); delete(editH); end
    0173     if ~isempty(buttonReturnH); delete(buttonReturnH); end
    0174     if ishghandle(dialogH); delete(dialogH); end
    0175     %set(buttonH, 'string', 'Recreate','Callback',@doButtonPress2);
    0176
    0177
    0178     % return data - or not
    0179     if nargout==0; clear data p; end
    0180
    0181     if VERBOSE; disp('returning from nt_dataview...'); end
    0182     %return;
    0183
    0184
    0185
    0186 %%%%%%%%%%%%%%%%%%%%%%%%   STRUCT  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0187 elseif isa(data, 'struct')
    0188     % struct in workspace
    0189     if VERBOSE; disp('struct'); end
    0190     if isempty(struct); error('''struct'' is empty!'); end
    0191     field_names=fieldnames(data);
    0192     if isempty(field_names); error('structure is empty!'); end
    0193     field_sizes=zeros(numel(field_names),1);
    0194     for k=1:numel(field_names);
    0195         field=getfield(data,field_names{k});
    0196         field_sizes(k)=round(prod(size(field))/1024);
    0197     end
    0198     clear field;
    0199     a=repmat(' (', numel(field_names),1);
    0200     b=cellstr(num2str(field_sizes, '%9d'));
    0201     b=char(b);
    0202     c=[repmat(' Mb)', numel(field_names),1)];
    0203     i=listdlg('liststring',cellstr([char(field_names),a,b,c]),...
    0204         'name', 'Select field in struct:', ...
    0205         'listsize', [600 300], ...
    0206         'OKstring','Select',...
    0207         'PromptString',p.data_name);
    0208
    0209     % call this function on the selected field
    0210     if i
    0211         data=getfield(data,field_names{i});
    0212         [p,data]=nt_dataview(data,p);
    0213         if nargout==0; data=[]; end
    0214         return
    0215     end
    0216
    0217
    0218
    0219 elseif isa(data, 'char') && ...
    0220         ( exist(data,'file')==2  ||  ...
    0221         (numel(data)>3 && all(data(end-2:end)=='.ds'))) % treat as file
    0222
    0223 %%%%%%%%%%%%%%%%%%%%%%%%   FILE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0224     fname=data;
    0225     if VERBOSE; disp('file'); end
    0226
    0227     if 0
    0228     % intercept mat files
    0229     if numel(fname)>4 & fname(end-3:end)=='.mat'
    0230         if VERBOSE; disp('mat file'); end
    0231         S=whos('-file',fname);
    0232         var_names=char(S.name);
    0233         var_sizes=round([S.bytes]/1024)';
    0234         a=repmat(' (', size(var_names,1),1);
    0235         b=cellstr(num2str(var_sizes, '%9d'));
    0236         b=char(b);
    0237         c=[repmat(' Mb)', size(var_names,1),1)];
    0238         i=listdlg('liststring',cellstr([var_names,a,b,c]),...
    0239             'name', 'Select variable in file:', ...
    0240             'listsize', [600 300], ...
    0241             'OKstring','Select',...
    0242             'PromptString',p.data_name);
    0243         if i
    0244             X=load(fname,var_names(i,:));
    0245             [p,data]=nt_dataview(X,p);
    0246         end
    0247         if nargout==0; data=[]; end
    0248         return
    0249     end
    0250     end
    0251
    0252     % hand over to data file reader
    0253     [p,data]=nt_read_data(fname);
    0254     [p,data]=nt_dataview(data,p);
    0255     return
    0256
    0257 %%%%%%%%%%%%%%%%%%%%%%%%   DIRECTORY  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0258 elseif isa(data, 'char') && exist(data, 'dir')==7
    0259     % directory
    0260     if VERBOSE; disp('directory'); end
    0261
    0262     d=dir(data);
    0263     if numel(d)==0
    0264         error(['directory >',data,'< not found']);
    0265     end
    0266     fnames=char(d.name);
    0267     idx=find(fnames(:,1)~='.');  % remove '.' and '..' and invisible files
    0268     d=d(idx);
    0269     fnames=fnames(idx,:);
    0270
    0271     % separate directories and files
    0272     didx=find([d.isdir]);
    0273     fidx=find(~[d.isdir]);
    0274     fnames=fnames([didx, fidx],:);
    0275
    0276    % count files within the directories
    0277     nfiles=zeros(numel(didx),1);
    0278     for k=1:numel(didx)
    0279         dd=dir([data,'/',d(didx(k)).name]);
    0280         fns=char(dd.name);
    0281         idx=find(fns(:,1)~='.');  % remove '.' and '..' and invisible files
    0282         nfiles(k)=numel(idx);
    0283     end
    0284
    0285     % size of the files
    0286     mbytes=[d(fidx).bytes]'/1024;
    0287
    0288     % string arrays to put in dialog list
    0289     a=repmat(' (', numel(d),1);
    0290     if numel(didx)>0
    0291         b=cellstr(num2str(nfiles, '%9d'));
    0292     else
    0293         b=[]; % stupid matlab!
    0294     end
    0295     if numel(fidx)>0
    0296         b=[b;cellstr(num2str(mbytes,'%0.1f'))];
    0297     end
    0298     b=char(b);
    0299     c=[repmat(' files)', numel(didx),1); repmat(' Mb)   ', numel(fidx),1)];
    0300
    0301     % which directory or file is user interested in?
    0302
    0303     i=listdlg('liststring',cellstr([fnames,a,b,c]),...
    0304         'name', 'Select file:', ...
    0305         'listsize', [300 300], ...
    0306         'OKstring','Select',...
    0307         'PromptString',p.data_name);
    0308
    0309     % call this function on that file
    0310     if i
    0311         data=strcat(data,'/',fnames(i,:));
    0312         [p,data]=nt_dataview(data,p);
    0313     end
    0314     return
    0315 else
    0316     disp([p.data_name,' not found']);
    0317     if nargout==0; data=[]; end
    0318     return
    0319 end
    0320
    0321
    0322 %h=data;
    0323 if nargout==0;
    0324     disp('hereiam');
    0325     clear data p
    0326 end
    0327
    0328
    0329
    0330
    0331 %%%%%%%%%%%%%%%%%%%%  LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0332     function doFigureKeyPress(obj, evd)  %#ok
    0333         switch(evd.Key)
    0334             case {'return','space'}
    0335                 returnPressed = true;
    0336             case 'escape'
    0337                 escapePressed=true;
    0338             otherwise
    0339                 otherKeyPressed=true;
    0340         end
    0341         uiresume(gcbf)
    0342     end
    0343
    0344     function doDelete(varargin)
    0345         delete(dialogH);
    0346     end
    0347
    0348     function editCallback(obj,evd);
    0349         %editString = get(obj,'String');
    0350         uiresume(gcbf);
    0351     end
    0352
    0353     function doButtonPress(obj,evd);
    0354         whichButton=obj;
    0355         uiresume(gcbf);
    0356     end
    0357     function doButtonPress2(obj,evd);
    0358         whichButton=obj;
    0359         p=get(gcf,'userdata');
    0360         evalin('caller',p.recreate);
    0361     end
    0362
    0363
    0364
    0365 end % this file's main function