1

I am looking for a one-liner to get multiple axes handles at once in an object array so that it can be passed into linkaxes. I am specifically looking for a single-line statement that does not use loops. I know I have done this in the past with a couple nested functions, but I can't figure out what I did.

The function gca can be used to get the axes handle for a specific figure, though this functionality does not appear to be documented. For example, call gca(3) to get the axis handle for Figure 3. I thought in the past I could call gca([1:4]) to get all four axes handles, but that does not seem to work.

I know that I can use get by calling get([1:4],'currentaxes'), which returns a cell array of axes handles. However, I have not figured out a way to convert a cell array of objects into an object array.

I'm using MATLAB R2015a.

David K
  • 1,296
  • 18
  • 39
  • Are the figures and/or axes being generated in such a way that you can't store their handles? – sco1 Jan 05 '16 at 18:20

4 Answers4

3

After building the axes:

figure(1); axes();
figure(2); axes();
figure(3); axes();

one-liners are fun:

linkaxes(arrayfun(@(k) get(k,'CurrentAxes'), 1:3));

Later Edit

Apparently this works only for old-style graphics (in which the handle of a graphic object is a double number). The new handle graphics (being proper objects) cannot be accumulated in a single array by arrayfun:

http://www.mathworks.com/help/matlab/graphics_transition/graphics-handles-are-now-objects-not-doubles.html

Maybe the much simpler one-liner:

linkaxes(findobj(1:3,'Type','Axes'));

would work...

  • I'm told I need to set `'UniformOutput'` to be `false`, which results in a cell array, and therefore not compatible with `linkaxes`. Does this not happen to you? – David K Jan 05 '16 at 17:26
  • @DavidK do you have figures without axes? If yes, remove them from the array (e.g. from 1:3). –  Jan 05 '16 at 17:27
  • @DavidK Also: does it work for the example code that I've given? –  Jan 05 '16 at 17:29
  • 1
    This doesn't work in MATLAB R2015b because: "matlab.graphics.axis.Axes output type is not supported. Set 'UniformOutput' to false.". – Thomas Ibbotson Jan 05 '16 at 17:29
  • @CST-Link All figures have axes, and no, the example code does not work. I forgot to mention that I'm using 2015a – David K Jan 05 '16 at 17:31
  • @DavidK I updated the answer. I'm curious if it works. –  Jan 05 '16 at 17:50
  • 1
    @DavidK Is not a competition, so it's fine. :-) I actually up-voted his answer after I posted my edit. –  Jan 05 '16 at 18:41
3

Beside the answers already posted, a possible one-line solution could be:

linkaxes(findobj('type','axes'))

it also allows implicitly prevent considering figure without axes.

Hope this helps.

il_raffa
  • 5,090
  • 129
  • 31
  • 36
2

How about this?

cell_of_axes = get([1:4], 'currentaxes');
array_of_axes = [cell_of_axes{:}];
linkaxes(array_of_axes);
Thomas Ibbotson
  • 735
  • 5
  • 9
  • Hmm, that does work, and is certainly better than a for loop, though I was hoping for a single statement. That may not be possible though. – David K Jan 05 '16 at 17:19
  • Yeah MATLAB syntax does not allow you to make that a one-liner as you can't add the {:} to the end of the call to get(...). – Thomas Ibbotson Jan 05 '16 at 17:22
  • 1
    @ThomasIbbotson [that's not strictly true](http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it) – sco1 Jan 05 '16 at 18:25
0

If you only want to link a specific subset of axes and they are stored as objects in a cell:

linkaxes([all_axes{:}], 'x');
Hoppo
  • 1,130
  • 1
  • 13
  • 32
leonos
  • 131
  • 7