tags:

views:

280

answers:

2

I have a 500-by-1 matrix of strings S and a 500-by-1 matrix of numbers N. I want to see them in the variable editor together like this:

S(1) N(1)
S(2) N(2)
...
S(500) N(500)

Is this possible?

+4  A: 

The following should allow you to look at the variables together in the Command Window:

disp([char(S) blanks(numel(N))' num2str(N)]);

The array S (which I presume is a cell array) is converted to a character array using the function CHAR. It's then concatenated with a column vector of blanks (made using the function BLANKS) and then a string representation of the numeric array N (made using the function NUM2STR). This is then displayed using the function DISP.

gnovice
Thank you for your help.
danatel
+2  A: 

Speaking narrowly to your question, just convert the numbers to cells. You'll have a single variable that the array editor can handle.

X = [ S num2cell(N) ];

More broadly, here's an array-oriented variant of sprintf that's useful for displaying records constructed from parallel arrays. You'd call it like this. I use something like this for displaying tabular data a lot.

sprintf2('%-*s  %8g', max(cellfun('prodofsize',S)), S, N)

Here's the function.

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
Andrew Janke
+1 for converting the numbers to cell arrays to view them in the variable editor. I never use the variable editor, so I just convert everything to strings to view in the command window (I don't use cells for that since cells containing long strings sometimes don't show their data or they screw up the formatting).
gnovice
Thank you, you aswer is closer to what I wanted.
danatel

related questions