我有 500-by-1 字符串S
矩阵和 500 x 1 数字矩阵N
。我想在变量编辑器中一起看到它们:
S(1) N(1)
S(2) N(2)
...
S(500) N(500)
这可能吗?
答案 0 :(得分:5)
以下内容应允许您在命令窗口中一起查看变量:
disp([char(S) blanks(numel(N))' num2str(N)]);
数组S
(我假设是一个单元格数组)使用函数CHAR转换为字符数组。然后将其与空白的列向量(使用函数BLANKS制作)连接,然后使用数字数组N
的字符串表示(使用函数NUM2STR制作)连接。然后使用函数DISP显示它。
答案 1 :(得分:3)
简单地谈谈您的问题,只需将数字转换为单元格即可。您将拥有阵列编辑器可以处理的单个变量。
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','cheese','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});
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" case
if iscell(args{i})
out{i} = args{i}{1};
else
out{i} = args{i};
end
else
% General case - kth element of array
if iscell(args{i})
out{i} = args{i}{k};
else
out{i} = args{i}(k);
end
end
end