在 MATLAB 中存储 imline 生成的对象的句柄
我试图在数组中存储一组对象句柄。这些对象是由 imline(.) 生成的一系列线条。我想存储句柄以便能够更改所需行的属性(在本例中为位置)。
我知道如何做到这一点 - 但是,当我尝试用线句柄填充矩阵时,会发生错误 - MATLAB 指出不可能从 IMLINE 转换为 DOUBLE。其他类型的对象不会发生这种情况。有办法绕过这个吗?
这是一些伪代码来澄清:
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]);
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]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
empty
静态方法创建类类型的空数组:Use the
empty
static method to create an empty array of the class type:您可能需要使用默认值行填充矩阵才能创建它。预分配大小为 N 的对象矩阵的典型方法是简单地将对象分配给矩阵中的最后一个元素。
注意,上面的行不适用于
imline
,因为它将为矩阵中的每个其他 N*N-1 imline 对象调用默认构造函数,并调用不带参数的imline
强制用户与当前轴交互。我的建议(如果您要预先分配)是在矩阵中显式定义所有默认行:
或者,您可以让 Matlab 为您填充数组。如果您发现经常需要此代码,请从
imline
派生一个新类。以下示例显示了至少需要发生的情况。它仅仅定义了一个构造函数。此示例还允许您将可选参数传递给imline
。如果未指定参数,则使用上述位置值创建imline
对象。用法示例:
数组中的最后一个
myimline
对象具有在赋值中指定的点,但其余元素具有如上所述的默认位置值[NaN NaN]
。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.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 ofimline
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:
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 toimline
as well. If no arguments are specified, theimline
object is created with position values as above.Example usage:
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.