matlab数字显⽰完整,如何在matlab中同时显⽰字符串和数
字?
狭隘地说你的问题,只是把数字转换成单元格。您将拥有⼀个数组编辑器可以处理的变量。
X = [ S num2cell(N) ];
更⼴泛地说,这⾥有⼀个⾯向数组的sprintf变体,它对于显⽰由并⾏数组构造的记录很有⽤。你可以这样称呼它。我经常⽤这种⽅法显⽰表格数据。
sprintf2('%-*s %8g', max(cellfun('prodofsize',S)), S, N)
功能如下。
function out = sprintf2(fmt, varargin)
%SPRINTF2 Quasi-"vectorized" sprintf
%
% out = sprintf2(fmt, varargin)
%
% Like sprintf, but takes arrays of arguments and returns cellstr. This
% lets you do formatted output on nonscalar arrays.
%
% Example:
% food = {'wine','chee','fancy bread'};
% price = [10 6.38 8.5];
% sprintf2('%-12s %6.2f', food, price)
% % Fancier formatting with width detection
% sprintf2('%-*s %6.2f', max(cellfun('prodofsize',food)), food, price)
[args,n] = promote(varargin);
out = cell(n,1);
for i = 1:n
argsi = grab(args, i);
out{i} = sprintf(fmt, argsi{:});
end
% Convenience HACK for display to command line
if nargout == 0腰肌锻炼
disp(char(out));
clear out;
end
function [args,n] = promote(args)
%PROMOTE Munge inputs to get cellstrs
for i = 1:numel(args)
if ischar(args{i})
args{i} = cellstr(args{i});4核电脑
end
end幼儿园国庆节
n = cellfun('prodofsize', args);
if numel(unique(n(n > 1))) > 1
error('Inconsistent lengths in nonscalar inputs');
end
月圆图片n = max(n);活着名句
function out = grab(args, k)
%GRAB Get the kth element of each arg, popping out cells for i = 1:numel(args)
if isscalar(args{i})古钱币博物馆
% "Scalar expansion" ca
if iscell(args{i})
out{i} = args{i}{1};
el
out{i} = args{i};
注意行人end
el
% General ca - kth element of array
if iscell(args{i})
out{i} = args{i}{k};
el
out{i} = args{i}(k);
end
end
end行书钢笔字