0

I want To write a Matlab function that can display one single image, multiple images (i.e. cell arrays, vector, matrices) of images so that I don't need to worry about writing 'plots', 'subplots', and other complicated Matlab mechanisms each time I run my program with new inputs. That is also one of my requirements in my project.

The following source code has several problems:

  1. it can't show only one image
  2. occasionally it doesn't preserve the original aspect ratio of images
  3. resizing the window to a smaller size drastically reduces the size of the images on display

How can I fix these issues?


Source Code

function draw_images(image_list)    
    d = size(image_list);
    l = length(d);

    figure;
    hold all
    colormap(gray(256));  

    % vector or cell-array
    if(l==2)
        N = length(image_list);
        [m, n] = factor_out(N);

        % images may be of differenet dimensions
        if(iscell(image_list))    
            for k=1:N
                h = subplot(m,n,k);
                image(image_list{k},'Parent',h);
                set(gca,'xtick',[],'ytick',[])
            end
        % must be of same dimension
        elseif(isvector(image_list))
            for k=1:N
                h = subplot(m,n,k);
                image(image_list(k),'Parent',h);
                set(gca,'xtick',[],'ytick',[])
            end
        end
    % 3D matrix of images (!!!)
    elseif(l==3)
        N = d(3) ;
        [m, n] = factor_out(N);
        for k=1:N
            I = image_list(:,:,k);
            subplot(m,n,k);
            imshow(I);
            set(gca,'xtick',[],'ytick',[])
        end  
    end     
    hold off

function [m, n] = factor_out(input_number)
    sqrtt = ceil(sqrt(input_number));    
    m = sqrtt;
    n = sqrtt;
user366312
  • 16,949
  • 65
  • 235
  • 452

1 Answers1

1

I think there is no a built-in solution similar to pyplot.tight-layout() in python. However, there exist several solutions written by several authors. I would prefer the function called subtightplot

There is also a discussion regarding this question at Stackoverflow called How to reduce the borders around subplots in matlab?

freude
  • 3,632
  • 3
  • 32
  • 51