view findFieldWidth.m @ 626:da30d3bbeea6 feature/grids

Avoid eval
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 20 Oct 2017 23:13:43 +0200
parents 30ae48efc7ae
children
line wrap: on
line source

% Calculates the maximum field width needed width fprintf for a given format fmt for the values in A
%     A = [1.13232 10.233 1.1];
%     width = findFieldWidth('%.2f',A);
% Gives wdith = 5
function width = findFieldWidth(fmt, A)
    width = 0;
    for i = 1:numel(A)
        elem = A(i);
        if iscell(elem)
            elem = elem{1};
        end
        str = sprintf(fmt,elem);
        width = max(width,length(str));
    end
end