Main Content

Graphics Arrays

Graphics arrays can contain the handles of any graphics objects. For example, this call to the plot function returns an array containing five line object handles:

y = rand(20,5);
h = plot(y)
h = 

  5x1 Line array:


This array contains only handles to line objects. However, graphics arrays can contain more than one type of graphics object. That is, graphics arrays can be heterogeneous.

For example, you can concatenate the handles of the figure, axes, and line objects into one array, harray:

hf = figure;
ha = axes;
hl = plot(1:10);
harray = [hf,ha,hl]
harray = 

  1x3 graphics array:

    Figure    Axes      Line

Graphics arrays follow the same rules as any MATLAB® array. For example, array element dimensions must agree. In this code, plot returns a 5-by-1 Line array:

hf = figure;
ha = axes;
hl = plot(rand(5));
harray = [hf,ha,hl];
Error using horzcat
Dimensions of matrices being concatenated are not consistent.

To form an array, you must transpose hl:

harray = [hf,ha,hl']
harray = 

  1x7 graphics array:

   Figure    Axes      Line      Line      Line      Line      Line

You cannot concatenate numeric data with object handles, with the exception of the unique identifier specified by the figure Number property. For example, if there is an existing figure with its Number property set to 1, you can refer to that figure by this number:

aHandle = axes;
ans = 

  1x2 graphics array:

    Axes      Figure

The same rules for array formation apply to indexed assignment. For example, you can build a handle array with a for loop:

harray = gobjects(1,7);
hf = figure;
ha = axes;
hl = plot(rand(5));
harray(1) = hf;
harray(2) = ha;
for k = 1:length(hl)
   harray(k+2) = hl(k);