Search code examples
matlabmatlab-figuremat

Matlab Extract images from a subplot in .fig format


How can I extract images from a subplot figure. fig with images plotted in it? See the subplot of the figure with images formated to png https://ibb.co/60jx8kX

I tried with this code but it didn't give me the output I needed.

fig = openfig( 'IC_01.fig' , 'new' , 'invisible' );
imgs = findobj(fig, 'type' , 'image' );
thiscmap = get(fig, 'colormap' );

for K = 1 : length(imgs)
    thisimg = get(imgs(K), 'CData' );
    % now do something with it for illustration purposes
    thisfilename = sprintf( 'extracted_image_%03d.jpg' , K);
    imwrite(thisimg, thiscmap, thisfilename);
end

Thank You.


Solution

  • here's a way to take the data from the first image to the image you want, the first lines are similar to what you did :

    f=openfig('IC_02.fig')
    imgs = findobj(f, 'type' , 'image' );
    for n=1:numel(imgs)
        C{n}=imgs(n).CData;
        X{n}=imgs(n).XData; 
        Y{n}=imgs(n).YData; 
        cmap{n}=imgs(n).Parent.Colormap; % not assuming it the same for all axes... 
    end
    

    Now some of the images will be the colorbar of the image you want (usually one after the other). We then take the Ydata of the colorbar to set the caxis limits of the image you want... for your case the data was in the 4th "image" object, and the limits are taken from the 3rd "image" (colorbar)...

    imagesc(X{4},Y{4},C{4})
    colormap(cmap{4})
    set(gca,'Ydir','normal')
    caxis([Y{3}(1) Y{3}(2)]) 
    

    enter image description here