function smart_imagesc(M,varargin)

%% Parse arguments
n_varargs = length (varargin);
expect_arg = false;
for i = 1:n_varargs
    arg = varargin{i};
    if ~expect_arg && ischar (arg) && arg (1) == '-'
        switch arg
            case '-scale',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    M_lims = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -scale missing.');
                end
            case '-colormap',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    c = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -colormap missing.');
                end
            case '-edgewidth',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    edgewidth = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -edgewidth missing.');
                end
            case '-edgecolor',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    edgecolor = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -edgecolor missing.');
                end
            case '-onlyhalf',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    whichhalf = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -onlyhalf missing.');
                end
            case '-includediag',
                if i + 1 <= n_varargs
                    expect_arg = true;
                    includediag = varargin{i + 1};
                else
                    error ('smart_imagesc:noarg', 'Required argument for -includediag missing.');
                end
                
            otherwise, error ('smart_imagesc:unknownarg', 'Unknown argument %q.', arg);
        end
    else
        if ~expect_arg
            error ('smart_imagesc:unexparg', 'Unexpected argument at position %d in varargin.', i);
        end
        expect_arg = false;
    end
end


if ~exist('c','var')
    c = colormap();
end

%c_N = size(c,1);
if ~exist('M_lims','var')
    M_lims = [ min(M(:)), max(M(:)) ];    
end

if ~exist('edgewidth','var')
    edgewidth=0.01;
    edgecolor = 'none';
end

if ~exist('edgecolor','var')
    edgecolor = 'k';
end


if ~exist('whichhalf','var')
    whichhalf = 'both';
end

if strcmp(whichhalf,'upper')
    do_half = 1;
elseif strcmp(whichhalf,'lower')
    do_half = 2;
else
    do_half = 0;
end

if ~exist('includediag','var')
    includediag = true;
end




x_n = size(M,2);
y_n = size(M,1);

rect_coord = @(x) [x(1)-0.5,x(2)-0.5,1,1];

%Mc = data_to_color(M, M_lims, c);

for x_i = 1:x_n
    for y_i = 1:y_n
        if (do_half==1 && y_i < x_i) || (do_half==2 && y_i > x_i) || (~includediag && x_i==y_i), continue; end
        rectangle('Position',rect_coord([x_i,y_i]),'edgecolor',edgecolor,'facecolor',data_to_color(M(x_i,y_i),M_lims,c),'linewidth',edgewidth);
    end
end
xlim([0.5,x_n+0.5])
ylim([0.5,y_n+0.5])
set(gca,'YDir','reverse')
    
end