No BSD License  

Highlights from
Display structures with differences

image thumbnail
from Display structures with differences by Alessandro Toso
Function to display in the command line array of structures and mark different field values.

display_structs( t )
function display_structs( t )

%
% function display_structs( t )
%
% This function takes and array of strutures as parameter and after
% searching differences in structures fields prints in a column format all
% the structures marking the differences

% check for correct argument class
if ~isstruct( t(1) )
    disp( ' Error! Argument is not a structure' );
    return;
end

% get filed names
fields   = fieldnames( t(1) );

% get number of fields
n_fields = length( fields );

% find differences in structures fileds
index =  find_struct_diff( t );

% print
for i = 1 : n_fields

    % print field index
    str = sprintf( '[%2d]%20s : ', i, char( fields( i ) ) );
    t_str = [];

    % keep track of struct fields
    s_fields = [];
    
    % format the output string
    for k = 1 : length( t )

        % current field
        c_field = t(k).( char( fields( i ) ) );
%         disp( char( fields( i )  ) )

        % format string for struct fields
        if isstruct( c_field )

            t_str = [str sprintf( '%20s  ', strcat( 'struct [', char( fields( i ) ) ,']' ) ) ];
            disp( t_str );
            s_fields = [s_fields i];
            index = -1;
            break;

        elseif isnumeric( c_field )
            % format string for numeric fields


            disp( str );
            display_struct_array( t, i );
            index = -1;
            break;

            % format string for char fields
        else

            t_str = [t_str sprintf( '%20s  ',c_field ) ];

        end
    end

    % mark differences is present
    if find( index == i )
        disp( strcat( str, t_str, '        <----- different' ) );
    elseif index ~= -1
        disp( strcat( str, t_str ) );
    end
end

function index = find_struct_diff( t )

%
% function index = find_struct_diff( t )
%
% This function takes and array of strutures and compares the same field to
% find differences
%

% return if the array has only one element
if length( t ) == 1
    index = [];
    return
end

s = 1;

% get field names
fields   = fieldnames( t(1) );

% get number of fields
n_fields = length( fields );

% search
for n = 1 : n_fields
    if check_field_diff( t, char( fields( n ) ) )
        index( s ) = n ;
        s = s + 1;
    end
end

return

function b = check_field_diff( t, field_name )

%
% function b = check_field_diff( t, field_name )
%
% Takes an array of structures 't' and check the differences in field
% 'field_name'. It returns 1 if there are some differences while 0 if all
% fields are equal
%

% return value initialization
b = 0;

% structure length
l = length( t );

% check structure length
if l == 1
    return;
end

% compare 'filed_name' in all structures
for k = 1 : l - 1
    for j = k + 1 : l
        if strcmp( t(j - 1).(field_name),  t(j).(field_name) ) ~= 1
            b = 1;
            return;
        end
    end
end

function display_struct_array( t, n )

% get filed names
fields   = fieldnames( t(1) );

% get structure length
l = length( t );

if ~isnumeric( t(1).( char( fields( n ) ) ) )
    return
end

% get array dimension
[ r, c ] = size( t(1).( char( fields( n ) ) ) );

for i = 1 : r
    for j = 1 : c
        
        str = [];
        
        for k = 1 : l
             if k == 1
                 str = sprintf( '                          [%2d-%2d] : %10f', i, j, t(k).( char( fields( n ) ) )(i,j) );
             else
                 str = strcat( str, sprintf('          : %10f', t(k).( char( fields( n ) ) )(i,j) ) );
             end
        end
        
        disp( str );
        
    end
end

Contact us at files@mathworks.com