Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

properties - Storing handles of objects generated by imline in MATLAB

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]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...