I am trying to store a set of object handles in an array. The objects are a series of lines generated by imline(.). I want to store the handles in order to be able to change
Use the empty
static method to create an empty array of the class type:
lines = imline.empty(0,10);
for idx=1:10
line = imline(gca, sortrows(rand(2,2)));
set(line,'UserData',idx)
lines(idx) = line;
end
You may need to fill your matrix with default valued lines in order to create it. The typical approach to preallocating a matrix of objects of size N
would be to simply assign an object to the last element in the matrix.
M(N,N)=imline(gca,[NaN NaN],[NaN NaN]); %# set non-displayable vals for x and y
NOTE, the line above will not work with imline
as it will call the default constructor for each of the other N*N-1 imline objects in the matrix and a call of imline
with no arguments forces user interaction with the current axis.
My advice (if you are pre-allocating) is to define all the default lines explicitly in the matrix:
for k=1:N*N
M(k)=imline(gca,[NaN NaN],[NaN NaN]);
end
%# Reshape (if necessary)
M = reshape(M,[N N]);
Alternatively, you could let Matlab fill the array for you. If you find that you will need this code often, derive a new class from imline
. The following example shows the very least that would need to happen. It merely defines a constructor. This example allows you to pass optional arguments to imline
as well. If no arguments are specified, the imline
object is created with position values as above.
classdef myimline<imline
methods
function obj = myimline(varargin)
if isempty(varargin)
varargin = {gca,[NaN NaN],[NaN NaN]};
end
obj = obj@imline(varargin{:});
end
end
end
Example usage:
%# Generate a 100 element array of `imline` objects,
%# but define the last one explicitly
mat(100)=myimline(gca,[0 1],[0 1]);
The last myimline
object in the array has points specified as in the assignment, but the rest of the elements have the default position values [NaN NaN]
as above.