MATLAB: How to get 4 large subfigures to be the same size when adding a colorbar to only two of them

MATLAB

I am trying to produce a figure like this one (apart from slightly larger digits on the colorbar and having all four images equidistant from one another by pulling them together vertically) to use in a LaTeX file:
I produced the inserted figure by taking a separate screenshot of each of the four images and the two colorbars. I then rescaled them once they were inserted to my LaTeX file. I have about 50 figures like this that I would like to make and it takes way too long to do it the way I did.
I spent about full 2 days trying to find a way around this by trying to use packages like tightfigure etc. but just cannot get it to work, which is why I decided to go for the tedious way described above. The main problems I encountered were:
  1. Getting the figures to be as close together as possible without making them really small (as this give bad resolution screenshots)
  2. Having the four plots all the same size and square-shaped despite there being a colorbar outside the two figures on the right
  3. I was also wondering if there is a better way to take the figure into LaTeX than screenshot-ing it?
I am really stuck and would appreciate any help with this! The code producing the 4 figures separately is the following:
x=linspace(0,1,201);
y=linspace(0,1,201);
for n = 999
a=zeros(201,4020);
cellsICC=a(:,202:402);
vessels1=a(:,3418:3618);
cellsPCC=a(:,1006:1206);
cellsECM=a(:,1810:2010);
cellsMMP2=a(:,2614:2814);
colorbarOn=1; %1 if want them; 0 if not
[I1,J1] = find(vessels1>0);
fh = figure;
ah = axes('Parent',fh);
imagesc(ah,x,y,cellsICC)
colormap(fh,flipud(bone));
set(ah,'XTick',[])
set(ah,'YTick',[])
set(ah, 'Box', 'on');
set(fh,'color','w');
axis square
if colorbarOn==1
cbh=colorbar ('eastoutside');
set(cbh,'YTick',0:1:max(max(max(cellsICC)),2));
end
view([0 90])
caxis(ah,[0 max(max(max(cellsICC)),2)])
shading flat
for i=1:length(I1)
line(x(J1(i)),y(I1(i)),'Color','Red','LineWidth',0.1,'Marker','.')
end
fh2 = figure;
ah2 = axes('Parent',fh2);
imagesc(ah2,x,y,cellsPCC)
colormap(fh2,flipud(bone));
set(ah2,'XTick',[])
set(ah2,'YTick',[])
set(ah2, 'Box', 'on');
set(fh2,'color','w');
axis square
if colorbarOn==1
cbh2=colorbar ('eastoutside');
set(cbh2,'YTick',0:1:max(max(max(cellsPCC)),4));
end
view([0 90])
caxis(ah2,[0 max(max(max(cellsPCC)),4)])
shading flat
for i=1:length(I1)
line(x(J1(i)),y(I1(i)),'Color','Red','LineWidth',0.1,'Marker','.')
end
fh3 = figure;
ah3 = axes('Parent',fh3);
imagesc(ah3,x,y,cellsECM)
colormap(fh3,flipud(bone));
set(ah3,'XTick',[])
set(ah3,'YTick',[])
set(ah3, 'Box', 'on');
set(fh3,'color','w');
axis square
if colorbarOn==1
cbh3=colorbar ('eastoutside');
set(cbh3,'YTick',[0 0.2 0.4 0.6 0.8 1]);
end
view([0 90])
caxis([0:1])
shading flat
for i=1:length(I1)
line(x(J1(i)),y(I1(i)),'Color','Red','LineWidth',0.1,'Marker','.')
end
fh4 = figure;
ah4 = axes('Parent',fh4);
imagesc(ah4,x,y,cellsMMP2)
colormap(fh4,flipud(bone));
set(ah4,'XTick',[])
set(ah4,'YTick',[])
set(ah4, 'Box', 'on');
set(fh4,'color','w');
axis square
if colorbarOn==1
cbh4=colorbar ('eastoutside');
set(cbh4,'YTick',0:0.1:max(max(max(cellsMMP2)),0.01));
end
view([0 90])
caxis([0 max(max(max(cellsMMP2)),0.01)])
shading flat
for i=1:length(I1)
line(x(J1(i)),y(I1(i)),'Color','Red','LineWidth',0.1,'Marker','.')
end
end
Many thanks for any help! I have tried very hard to find a solution to this…

Best Answer

Try getting the Position (or InnerPosition or OuterPosition) property of the axes with the color bar and then setting the same property of the other axes to that.
handles.axesWithoutColorBar.Position(3) = handles.axesWithColorBar.Position(3); % Set width
handles.axesWithoutColorBar.Position(4) = handles.axesWithColorBar.Position(4); % Set height