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: This function is called by:

SUBFUNCTIONS ^

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

Generated on Mon 10-Nov-2014 14:40:42 by m2html © 2005