user2861089
user2861089

Reputation: 1235

Matlab axis error using imagesc

I am plotting multiple heatmaps in matlab 3014b using imagesc with one common colorbar. Here is my code:

a(1)= subplot('Position',[0.1, 0.65, 0.3, 0.3]);
data1 = rand(5);
imagesc(data1)
ax = gca; 
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};

a(2)= subplot('Position',[0.45, 0.65, 0.3, 0.3]);
data2 = rand(5);
imagesc(data2)
ax = gca; 
ax.XTick = [1 2 3 4 5 6];
ax.XTickLabel = {'0','0.1', '0.2', '0.3','0.4','0.5'};
ax.YTick = [1 2 3 4 5 6];
ax.YTickLabel = {'1','10', '100', '1000', '10000', '100000'};

h=colorbar;
set(h, 'Position', [.8 .135 .0581 .8150])
for i=1:2
      pos=get(a(i), 'Position');
      set(a(i), 'Position', [pos(1) pos(2)]);
end

But I get the following error:

Error using matlab.graphics.axis.Axes/set
While setting the 'Position' property of Axes:
Value must be a 4 element vector

Not exactly sure how to resolve this? Thanks!

Upvotes: 0

Views: 373

Answers (1)

rayryeng
rayryeng

Reputation: 104474

Error is right here:

for i=1:2
      pos=get(a(i), 'Position');
      set(a(i), 'Position', [pos(1) pos(2)]); %// <--- here
end

Is there a particular reason why you're truncating the last two elements? Position should be a 4 element vector where the first two elements define the distance from the lower-left corner of the container to the lower-left corner of the axes, and the third and fourth element are the width and height of the axes within the window. If it's your intention to perhaps keep all of the axes the same width/height, do something like this:

for i=1:2
      pos=get(a(i), 'Position');
      set(a(i), 'Position', [pos(1) pos(2) 1 1]); %// Change
end

Check out the documentation on MathWorks on axes properties... specifically Position here: http://www.mathworks.com/help/matlab/ref/axes-properties.html#zmw57dd0e52524

Upvotes: 1

Related Questions