I subplotted my graphs using command subplot(1,2,1) and subplot(1,2,2). As you see I have 2 graphs in the figure. I want to relocation graphs like first graph should be in the second graph's location and second graph should be in the first graph location.
Asked
Active
Viewed 90 times
1 Answers
1
I am assuming you already plotted the data and can not plot them again. Then you can get the axes handles, i.e. the pointers to each subplot by finding all axes objects in the current figure (gcf
), which are not legends and not colorbars. (This findobj
command is taken from an answer to this question by Nzbuu)
ax = findobj(gcf,'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
To get the positions of the current axes, you can read the (well...) 'Position'
property of the axes object:
pos = get(ax,'Position');
which returns a 2x1 cell array, where each entry is a 1x4 position vector of the corresponding axis. Now we just have to switch the two positions:
set(ax(1),'Position',pos{2});
set(ax(2),'Position',pos{1});
And that's it, the two subplots are switched.
-
Thanks for you answer, but I obtained 4x1 cell array where each entry is a 1x4 position vector. – Deniz Baturay Oct 21 '15 at 10:36
-
Do you have four subplots in the figure? Or do you have multiple figures open? – hbaderts Oct 21 '15 at 11:34
-
No, I have 2 subplots. I mean there is 2 plots in 'Figure 1'. Only Figure 1 is open. – Deniz Baturay Oct 21 '15 at 13:43
-
What is the dimension of `ax`? If you `close all` and run `subplot(121);plot(1:5);subplot(122);plot(5:-1:1);` and then do the steps above - what happens? – hbaderts Oct 21 '15 at 14:37
-
@ hbaderts I understood the problem. There is a legend in '1' position in the two graphs because of that I obtained 4x1 ax. Is it any way to do this with legends? – Deniz Baturay Oct 22 '15 at 07:58
-
@DenizBaturay oh that was the problem, thanks for figuring that out. I edited the answer to allow legends and colorbars in the plot. – hbaderts Oct 22 '15 at 09:35