⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showcell.m

📁 实现地震勘探中
💻 M
📖 第 1 页 / 共 2 页
字号:
function showcell(varargin)
%SHOWCELL   Displays cell array with long strings in the command window.
%   SHOWCELL(A) displays the contents of a cell array A in the command
%   window.  It will format the display so that long strings will display
%   appropriately.  A can be a cell array of numbers, strings, and/or other
%   objects.
%
%   Typically, if a cell array contains long strings, it will not display
%   the text:
%
%   >> A
% 
%   A = 
% 
%       [3]    'this is a text.'    'hello'
%       [4]    'More text'          [   32]
%       [6]          [1x54 char]    [   53]
% 
%   SHOWCELL will display it properly:
%
%   >> showcell(A)
%     [ 3]    'this is a text.'                                           'hello'
%     [ 4]    'More text'                                                 [32]
%     [ 6]    'This is a very long text that may not show up properly'    [53]
%
%   Acceptable numbers are of class DOUBLE, SINGLE, LOGICAL, UINT8, UINT16,
%   UINT32, UINT64, INT8, INT16, INT32, INT64. Elements other than CHAR or
%   numbers are displayed as the size and name of the object,
%     e.g. [1x1 struct]
%
%   SHOWCELL(A,'option1',value1,...) specifies optional arguments passed
%   in in pairs.  Valid options are (abbreviated names accepted):
%
%      'spacing'   - column spacing.  Default is 4 spaces.
%      'numformat' - number of digits OR format string (see SPRINTF) for
%                    numerical values.  Default is 5 digits.
%
%   Example:
%       showcell(A, 'spacing', 5);
%       showcell(A, 'numformat', 3);
%       showcell(A, 'n', '%0.4f');
%       showcell(A, 'sp', 2, 'nu', 6);
%
%   See also DISP, DISPLAY
%
%
%   VERSIONS:
%     v1.0 - first version
%     v1.1 - add quotes around strings (Jan 2006)
%     v1.2 - accepts uint8, uint16, uint32, uint64, int8, int16, int32,
%             int64, single, double, logical for numeric values.
%     v2.0 - each column does not have to be of the same class. the cell
%             elements can be of any class. (Jan 2006)
%     v2.1 - fixed problems with displaying empty cell elements. (Jan 2006)
%     v2.2 - fixed displaying an empty cell {}. Remove MORE function, since
%             this can be achieved externally by calling MORE. (Jan 2006)
%     v2.3 - now displays multi-dimension cells (Feb 10, 2006)
%

%   Jiro Doke
%   June 2004

%-----------------------------------------------------------------------
% Check cell array                  
%-----------------------------------------------------------------------
if ~nargin              
   return;              
end                     
                        
arg = varargin{1};                  
                        
if ~iscell(arg)                     
   error('This is not a cell array.');          
end                     
                        
%-----------------------------------------------------------------------
% Parse optional arguments          
%-----------------------------------------------------------------------
                        
% Default values                    
num_spaces = 4;                     
num_digits = 5;                     
                        
% Possible optional arguments                   
optSpacing   = 'spacing  ';         
optNumformat = 'numformat';         
                        
if nargin > 1                       
   vars = varargin(2 : end);                    
                        
   if mod(length(vars) , 2)         
     error('The optional arguments must come in pairs.');   
   end                  
                        
  for id = 1 : 2 : length(vars)                 
                        
    % Get number of characters provided for optional arguments          
    % Accepts abbreviated option names          
     varC = min([length(vars{id}), 9]);         
                        
     switch lower(vars{id})         
        case optSpacing(1 : varC)     % SPACING             
           if isnumeric(vars{id + 1})           
              num_spaces = round(vars{id + 1});             
           else                     
              error('Bad value for SPACING. Must be an integer');       
           end                      
                        
      case optNumformat(1 : varC)   % NUMFORMAT             
         if isnumeric(vars{id + 1})             
            num_digits = round(vars{id + 1});               
         else                       
            num_digits = vars{id + 1};          
         end            
                        
      otherwise                     
        error('Unknown option.');               
      end               
   end                  
end                     
                        
%-----------------------------------------------------------------------
% Deal with multi-dimension cells               
%-----------------------------------------------------------------------
                        
isLoose = isequal(get(0,'FormatSpacing'),'loose');          
                        
if ndims(arg) > 2                   
   sz = size(arg);                  
   id = cell(ndims(arg) - 2, 1);                
else                    
   sz = [0 0 1];                    
end                     
                        
for ii = 1:prod(sz(3:end))          
   if exist('id', 'var')            
      [id{:}]  = ind2sub(sz(3:end), ii);      %#ok  
      str      = ['(:,:', sprintf(',%d', id{:}), ')'];      
      this_arg = arg(:, :, id{:});              
   else                 
      this_arg = arg;               
      str      = '';                
   end                  
   if ~isempty(inputname(1))                    
      if isLoose                    
         disp(' ');                 
         fprintf('%s%s =\n', inputname(1), str);            
         disp(' ');                 
      else              
         fprintf('%s%s =\n', inputname(1), str);            
      end               
   end                  
                        
   if isequal(size(this_arg), [0 0])            
      disp('     {}');              
      if isLoose
         disp(' ');
      end         %
   elseif ismember(0, size(this_arg))           
      fprintf('   Empty cell array: %d-by-%d\n', size(this_arg));       
       if isLoose
          disp(' ')
       end         %
   else                 
       showcellEngine(this_arg, num_spaces, num_digits);    
   end                  
end                     
                        
                        
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
% showcellEngine                    
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------

function showcellEngine(arg, num_spaces, num_digits)
                        
%-----------------------------------------------------------------------
% Determine class of cell elements              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -