Storing handles of objects generated by imline in MATLAB

旧时模样 提交于 2019-11-28 05:34:01

问题


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]);

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6806344/storing-handles-of-objects-generated-by-imline-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!