Storing handles of objects generated by imline in MATLAB

前端 未结 2 565
遥遥无期
遥遥无期 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: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

    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.

提交回复
热议问题