Home > NoiseTools > nt_dataview_old.m

# nt_dataview_old

## PURPOSE

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

## SYNOPSIS

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

## DESCRIPTION

    [data,p]=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 [data,p]=nt_dataview(data,p) - view data sets
- nt_whoss size=nt_whoss - total Gbytes used by variables

This function is called by:

## SUBFUNCTIONS

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

## SOURCE CODE

    0001 function [data,p]=nt_dataview(data,p)
    0002 %[data,p]=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
    0010 verbose=1;
    0011 evalError=0;
    0012 if verbose; nt_whoss; drawnow; end
    0013 set(0, 'DefaultUICOntrolFontSize', 14)
    0014
    0015 if nargin < 1; error('!'); end
    0016 if nargin < 2; p=[]; end
    0017
    0018
    0019 if isempty(data); data=[]; return; end
    0020
    0021 % determine the name to display on dialog or window
    0022 if isa(data,'char');
    0023     data_name=data;
    0024 elseif isa(data,'struct');
    0025     data_name='structure';
    0026 elseif isa(data,'double');
    0027     data_name='matrix';
    0028 else
    0029     error('argument should be string, struct, or double');
    0030 end
    0031
    0032 %%%%%%%%%%%%%%%%%%%%%%%%   MATRIX  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0033 if isa(data, 'double')
    0034     % matrix in workspace
    0035     if verbose; disp('matrix'); end
    0036
    0037     % put up dialog to allow user to manipulate matrix before plotting
    0038     s=size(data);
    0039     sstring=['[ ',num2str(s(1))];
    0040     for k=2:numel(s); sstring=[sstring,', ', num2str(s(k))]; end
    0041     sstring=[sstring,' ]'];
    0042     fPos    = get(0,'DefaultFigurePosition');
    0043     fPos([3,4]) = [500, 200];
    0044     whichButton=[];
    0045     defaultPressed=[];
    0046     escapePressed=[];
    0047     plotData=0;
    0048     imageData=0;
    0049     dialogH=figure('Visible','on', 'WindowStyle','modal', 'Name','Data'                  , ...
    0050         'NumberTitle'     ,'off'                       ,...
    0051         'Pointer'         ,'arrow'                    , ...
    0052         'Position'        ,fPos                     , ...
    0053         'color', [1 1 1]                  ,...
    0054         'KeyPressFcn'     ,@doFigureKeyPress          , ...
    0055         'ButtonDownFcn'   ,@doDelete                  , ...
    0056         'IntegerHandle'   ,'off'                      , ...
    0057         'HandleVisibility','callback'                 , ...
    0058         'CloseRequestFcn' ,@doDelete                  ...
    0059         );
    0060     textAxesH=axes('visible', 'off', 'parent', dialogH);
    0061     textH=text(0.01, 0.9, ['size(data) = ', sstring], 'parent', textAxesH,'fontsize', 14, 'fontweight', 'bold');
    0062     editString='[data,p]=nt_dataview(data,p);';
    0063     editH2=uicontrol('Style','edit','String',editString,'position', [20 100 fPos(3)-50 30], 'parent', dialogH,...
    0064         'callback', @editCallback, 'foregroundcolor',[1 1 1]*0 );
    0065     buttonH1=uicontrol('style', 'pushbutton', 'string', 'Execute String, Return ''data''','position', [50, 60, 200, 25],...
    0066          'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
    0067          'fontSize', 14);
    0068     buttonH2=uicontrol('style', 'pushbutton', 'string', 'Imagesc','position', [50, 20, 100, 25],...
    0069          'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
    0070          'fontSize', 14);
    0071     buttonH3=uicontrol('style', 'pushbutton', 'string', 'Plot','position', [400 20, 80, 25],...
    0072         'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', dialogH,...
    0073         'fontSize', 14);
    0074     if ~ ishghandle(dialogH); return; end
    0075     uiwait(dialogH);
    0076     if ~ ishghandle(dialogH); return; end
    0077     if defaultPressed %
    0078         plotData=true;
    0079     elseif escapePressed % return
    0080         close(dialogH);
    0081     else
    0082         h=get(dialogH,'CurrentObject');
    0083         if h==editH2
    0084             close(dialogH);
    0085             disp('evaluate...');
    0086             try
    0087                 eval(editString, 'evalError=true;');
    0088             catch
    0089                 error('!!');
    0090             end
    0091             disp('returned');
    0092         else
    0093             switch whichButton
    0094                 case buttonH1 % evaluate & return
    0095                     close(dialogH);
    0096                     disp('evaluate...');
    0097                     eval(editString, 'evalError=true;');
    0098                     disp('returned');
    0099                 case buttonH2 % plot
    0100                     imageData=true;
    0101                 case buttonH3 % plot
    0102                     plotData=true;
    0103                 otherwise
    0104                     error('!');
    0105             end
    0106         end
    0107     end
    0108
    0109     if evalError
    0110         disp('  ');
    0111         disp('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    0112         disp('error while evaluating user-input string:');
    0113         disp(' ');
    0114         disp(['  >',editString,'<'])
    0115         disp(' ');
    0116         disp(lasterr);
    0117         disp('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    0118         disp(' ');
    0119         return;
    0120     end
    0121
    0122
    0123     if ishghandle(dialogH); close(dialogH); end
    0124     if plotData || imageData
    0125         if ~isempty(get(0,'currentfigure'));
    0126             plotH=figure(gcf); clf
    0127         else
    0128             plotH=figure;
    0129         end
    0130         set(plotH,'position', [0 100, 1000, 400]);
    0131         set(plotH,'color',[1 1 1]);
    0132         buttonH4=uicontrol('style', 'pushbutton', 'string', 'Return Data','position', [50, 20, 100, 25],...
    0133         'KeyPressFcn',@doControlKeyPress, 'Callback',@doButtonPress, 'HorizontalAlignment','center', 'parent', plotH,...
    0134         'fontSize', 14);
    0135         drawnow
    0136         axes('position',[.05 .25 .92 .7], 'parent', plotH);
    0137         set(gca,'fontsize',12);
    0138         if plotData
    0139             plot(data);
    0140         elseif imageData
    0141             imagescc(data');
    0142         end
    0143         xlim([1 size(data,1)]); xlabel('samples');
    0144         title(['matrix: ', inputname(1)]);
    0145         uiwait;
    0146         switch whichButton
    0147              case buttonH4 % return
    0148                  close(plotH);
    0149             otherwise
    0150                 error('!');
    0151         end
    0152     end
    0153     disp('end of plotting');
    0154
    0155     if nargout==0; clear data, p; end
    0156     if verbose; disp('returning from nt_dataview...'); end
    0157     return;
    0158
    0159
    0160
    0161 %%%%%%%%%%%%%%%%%%%%%%%%   STRUCT  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0162 elseif isa(data, 'struct')
    0163     % struct in workspace
    0164     if verbose; disp('struct'); end
    0165     field_names=fieldnames(data);
    0166     if isempty(field_names); error('structure is empty!'); end
    0167     field_sizes=zeros(numel(field_names),1);
    0168     for k=1:numel(field_names);
    0169         field=getfield(data,field_names{k});
    0170         field_sizes(k)=round(prod(size(field))/1024);
    0171     end
    0172     clear field;
    0173     a=repmat(' (', numel(field_names),1);
    0174     b=cellstr(num2str(field_sizes, '%9d'));
    0175     b=char(b);
    0176     c=[repmat(' Mb)', numel(field_names),1)];
    0177     i=listdlg('liststring',cellstr([char(field_names),a,b,c]),...
    0178         'name', 'Select field in struct:', ...
    0179         'listsize', [600 300], ...
    0180         'OKstring','Select',...
    0181         'PromptString',data_name);
    0182
    0183     % call this function on the selected field
    0184     if i
    0185         data=nt_dataview(getfield(data,field_names{i}),p);
    0186         if nargout==0; data=[]; end
    0187         return
    0188     end
    0189
    0190
    0191 elseif isa(data, 'char') && exist(data,'file')==2
    0192
    0193 %%%%%%%%%%%%%%%%%%%%%%%%   FILE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0194     if verbose; disp('file'); end
    0195     try
    0196         % .mat file? list variables
    0197         S=whos('-file',data);
    0198         var_names=char(S.name);
    0199         var_sizes=round([S.bytes]/1024)';
    0200         a=repmat(' (', size(var_names,1),1);
    0201         b=cellstr(num2str(var_sizes, '%9d'));
    0202         b=char(b);
    0203         c=[repmat(' Mb)', size(var_names,1),1)];
    0204         i=listdlg('liststring',cellstr([var_names,a,b,c]),...
    0205             'name', 'Select variable in file:', ...
    0206             'listsize', [600 300], ...
    0207             'OKstring','Select',...
    0208             'PromptString',data_name);
    0209         if i
    0210             X=load(data,var_names(i));
    0211             data=nt_dataview(X,p);
    0212         end
    0213         if nargout==0; data=[]; end
    0214         return
    0215     catch ME
    0216         %ME
    0217     end
    0218
    0219     % select file reader among those available
    0220     if 2==exist('ft_read_header');
    0221         try
    0222             data=ft_read_header(data);
    0223             x=ft_read_data(data)';
    0224             h=nt_dataview(x,p);
    0225         catch
    0226             disp(['FieldTrip cannot read file >',data,'<']);
    0227         end
    0228     else
    0229         error('function ft_read_header() not found: download FieldTrip and/or adjust path');
    0230     end
    0231
    0232 %%%%%%%%%%%%%%%%%%%%%%%%   DIRECTORY  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0233 elseif isa(data, 'char') && exist(data, 'dir')==7
    0234     % directory
    0235     if verbose; disp('directory'); end
    0236
    0237     d=dir(data);
    0238     fnames=char(d.name);
    0239     idx=find(fnames(:,1)~='.');  % remove '.' and '..' and invisible files
    0240     d=d(idx);
    0241     fnames=fnames(idx,:);
    0242
    0243     % separate directories and files
    0244     didx=find([d.isdir]);
    0245     fidx=find(~[d.isdir]);
    0246     fnames=fnames([didx, fidx],:);
    0247
    0248    % count files within the directories
    0249     nfiles=zeros(numel(didx),1);
    0250     for k=1:numel(didx)
    0251         dd=dir([data,'/',d(didx(k)).name]);
    0252         fns=char(dd.name);
    0253         idx=find(fns(:,1)~='.');  % remove '.' and '..' and invisible files
    0254         nfiles(k)=numel(idx);
    0255     end
    0256
    0257     % size of the files
    0258     mbytes=[d(fidx).bytes]'/1024;
    0259
    0260     % string arrays to put in dialog list
    0261     a=repmat(' (', numel(d),1);
    0262     if numel(didx)>0
    0263         b=cellstr(num2str(nfiles, '%9d'));
    0264     else
    0265         b=[]; % stupid matlab!
    0266     end
    0267     if numel(fidx)>0
    0268         b=[b;cellstr(num2str(mbytes,'%0.1f'))];
    0269     end
    0270     b=char(b);
    0271     c=[repmat(' files)', numel(didx),1); repmat(' Mb)   ', numel(fidx),1)];
    0272
    0273     % which directory or file is user interested in?
    0274
    0275     i=listdlg('liststring',cellstr([fnames,a,b,c]),...
    0276         'name', 'Select file:', ...
    0277         'listsize', [300 300], ...
    0278         'OKstring','Select',...
    0279         'PromptString',data_name);
    0280
    0281     % call this function on that file
    0282     if i
    0283         data=strcat(data,'/',fnames(i,:));
    0284         data=nt_dataview(data,p);
    0285     end
    0286 else
    0287     disp([data_name,' not found']);
    0288     if nargout==0; data=[]; end
    0289     return
    0290 end
    0291
    0292
    0293 %h=data;
    0294 if nargout==0; data=[]; end
    0295
    0296 %%%%%%%%%%%%%%%%%%%%  LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0297     function doFigureKeyPress(obj, evd)  %#ok
    0298         switch(evd.Key)
    0299             case {'return','space'}
    0300                 defaultPressed = true;
    0301             case 'escape'
    0302                 escapePressed=true;
    0303         end
    0304         uiresume(gcbf)
    0305     end
    0306
    0307     function doDelete(varargin)
    0308         delete(dialogH);
    0309     end
    0310
    0311     function editCallback(obj,evd);
    0312         editString = get(obj,'String');
    0313         uiresume(gcbf);
    0314     end
    0315
    0316     function doButtonPress(obj,evd);
    0317         whichButton=obj;
    0318         uiresume(gcbf);
    0319     end
    0320
    0321
    0322
    0323 end % this file's main function