Storing handles of objects generated by imline in MATLAB

前端 未结 2 557
遥遥无期
遥遥无期 2020-12-20 00:27

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

相关标签:
2条回答
  • 2020-12-20 01:05

    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
    

    enter image description here

    0 讨论(0)
  • 2020-12-20 01:30

    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.

    0 讨论(0)
提交回复
热议问题