📄 showcell.m
字号:
%-----------------------------------------------------------------------
cellArg = arg(:);
isNumChar = false(length(cellArg), 12);
isNumChar(:, 1) = cellfun('isclass', cellArg, 'char' );
isNumChar(:, 2) = cellfun('isclass', cellArg, 'double' );
isNumChar(:, 3) = cellfun('isclass', cellArg, 'single' );
isNumChar(:, 4) = cellfun('isclass', cellArg, 'uint8' );
isNumChar(:, 5) = cellfun('isclass', cellArg, 'uint16' );
isNumChar(:, 6) = cellfun('isclass', cellArg, 'uint32' );
isNumChar(:, 7) = cellfun('isclass', cellArg, 'uint64' );
isNumChar(:, 8) = cellfun('isclass', cellArg, 'int8' );
isNumChar(:, 9) = cellfun('isclass', cellArg, 'int16' );
isNumChar(:, 10) = cellfun('isclass', cellArg, 'int32' );
isNumChar(:, 11) = cellfun('isclass', cellArg, 'int64' );
isNumChar(:, 12) = cellfun('isclass', cellArg, 'logical');
% Number of elements in cell element
numElmt = cellfun('prodofsize', cellArg);
% Remove number cells with vectors (more than a scalar)
isNumChar(:, 2:end) = isNumChar(:, 2:end) & repmat(numElmt <= 1, 1, 11);
% Number elements
isNum = ~~sum(isNumChar(:, 2:end), 2);
% Cell elements
cellElements = cellfun('isclass', cellArg, 'cell');
% Empty elements
emptyElements = cellfun('isempty', cellArg);
emptyCells = emptyElements & cellElements;
emptyNums = emptyElements & isNum;
% All other objects (including objects with more than one element)
isObj = xor(emptyCells, ~sum(isNumChar, 2));
% Discard empty number elements. These will be processed separately.
isNumChar(isNumChar & repmat(emptyNums, 1, size(isNumChar, 2))) = false;
%-----------------------------------------------------------------------
% Deal with empty elements
%-----------------------------------------------------------------------
if any(emptyCells)
cellArg(emptyCells) = {'{}'};
end
if any(emptyNums)
cellArg(emptyNums) = {'[]'};
end
%-----------------------------------------------------------------------
% Deal with numeric elements
%-----------------------------------------------------------------------
numID = logical(sum(isNumChar(:, 2:end), 2));
if ~isempty(find(numID,1))
TOdouble = repmat(NaN, length(cellArg), 1);
% Convert the numeric/logical values to double
useIDX = find(sum(isNumChar(:, 2:end)));
% Only parse through valid types
for iType = useIDX + 1
TOdouble(isNumChar(:, iType), 1) = ...
double([cellArg{isNumChar(:, iType)}]');
end
TOdouble(~numID) = [];
% Convert DOUBLE to strings and put brackets around them
try
tmp = strcat({'['}, num2str(TOdouble, num_digits), {']'});
catch % ME
% getReport(ME)
error('Abnormal termination')
end
cellArg(numID) = tmp;
end
%-----------------------------------------------------------------------
% Deal with string elements
%-----------------------------------------------------------------------
% Put single quotes around the strings
stringCell = strcat({''''}, cellArg(isNumChar(:, 1)), {''''});
cellArg(isNumChar(:, 1)) = stringCell;
%-----------------------------------------------------------------------
% Deal with elements other than string or numeric
%-----------------------------------------------------------------------
objID = find(isObj);
objCell = cell(length(objID), 1);
for iObj = 1:length(objID)
sz = size(cellArg{objID(iObj)});
cl = class(cellArg{objID(iObj)});
% Display size and class type, wrapped by brackets
switch cl
case 'cell'
if length(sz) < 4
objCell{iObj} = ['{', sprintf('%dx', sz(1:end-1)), ...
num2str(sz(end)), sprintf(' %s}', cl)];
else
objCell{iObj} = sprintf('{%d-D %s}', length(sz), cl);
end
otherwise
if length(sz) < 4
objCell{iObj} = ['[', sprintf('%dx', sz(1:end-1)), ...
num2str(sz(end)), sprintf(' %s]', cl)];
else
objCell{iObj} = sprintf('[%d-D %s]', length(sz), cl);
end
end
end
cellArg(isObj) = objCell;
% Reconstruct the original size
arg = reshape(cellArg, size(arg));
%-----------------------------------------------------------------------
% Create FPRINTF format string based on length of strings
%--------------------------------------------------------------------------
char_len = cellfun('length', arg); %
if 0 % Change this to 1 in order to right justify numeric elements.
% This will be slightly slower.
conv_str = ' ';
for iCol = 1:size(arg, 2);
if length(unique(char_len(:, iCol))) == 1
conv_str = [conv_str, ...
sprintf('%%-%ds%s', unique(char_len(:, iCol)), ...
blanks(num_spaces))]; %#ok Don't bother ...
else
tmp = char(arg(:, iCol));
idx1 = strfind(tmp(:, 1)', '[');
idx2 = strfind(tmp(:, 1)', '{');
tmp([idx1 idx2], :) = strjust(tmp([idx1 idx2], :), 'right');
arg(:, iCol) = cellstr(tmp);
conv_str = [conv_str, ...
sprintf('%%-%ds%s', max(char_len(:, iCol)), ...
blanks(num_spaces))]; %#ok Don't bother ...
end
end
else
% Create array of max character lengths and blank pads
char_max = [num2cell(max(char_len, [], 1)); ...
repmat({blanks(num_spaces)}, 1, size(char_len, 2))];
conv_str = [' ', sprintf('%%-%ds%s', char_max{:})];
end
% Add carrige return at the end
conv_str = [conv_str(1 : end - num_spaces) '\n'];
%--------------------------------------------------------------------------
% Display in command window
%--------------------------------------------------------------------------
% Must transpose for FPRINTF to work
arg = arg';
% If arg is a single EMPTY cell/string/numeric element,
% then wrap it with {}
if length(arg) == 1
switch arg{1}
case {'{}', '''''', '[]'}
conv_str = ' {%s}\n';
end
end
try
% Wrap around TRY ... END in case the user quits out of MORE
fprintf(1, conv_str, arg{:});
if isequal(get(0,'FormatSpacing'),'loose')
disp(' ');
end
catch
% Do nothing
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -