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 the property of a desired line (in this case, position).
I know how to do this – however, when I try to fill a matrix with the handles of lines, an error occurs – MATLAB states that conversion from IMLINE to DOUBLE is not possible. This does not happen with other types of objects. Is there a way to circumvent this?
Here is some pseudocode to clarify:
lines=zeros(1,x); % defining empty storage matrix
for idx=1:x
line=imline(ax_handl,[x_1 y_1; x_2 y_2])
set(line,'UserData',idx) % in order to identify independent lines with the number
lines(idx)=line; % here I try to store a line handle as it's made
end
% now in the function responsible for motion of objects, I assign new position to line
line_num=get(gco,'UserData'); % this relates other objects associated with line number
setPosition(lines(line_num),[a b; c d]);
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
Nwould be to simply assign an object to the last element in the matrix.NOTE, the line above will not work with
imlineas it will call the default constructor for each of the other N*N-1 imline objects in the matrix and a call ofimlinewith 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:
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 toimlineas well. If no arguments are specified, theimlineobject is created with position values as above.Example usage:
The last
myimlineobject 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.