在MATLAB中存储由imline生成的对象的句柄 [英] Storing handles of objects generated by imline in MATLAB
问题描述
我试图在数组中存储一组对象句柄.对象是由imline(.)生成的一系列线.我想存储这些句柄,以便能够更改所需行的属性(在本例中为position).
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).
我知道该怎么做-但是,当我尝试用线柄填充矩阵时,会发生错误-MATLAB指出无法从IMLINE转换为DOUBLE.其他类型的对象则不会发生这种情况.有办法避免这种情况吗?
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]);
推荐答案
您可能需要使用默认值的行填充矩阵以创建矩阵.预分配大小为N
的对象矩阵的典型方法是简单地将一个对象分配给矩阵中的最后一个元素.
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
注意,上面的行不适用于imline
,因为它将为矩阵中的每个其他N * N-1个imline对象调用默认构造函数,并调用
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]);
或者,您可以让Matlab为您填充数组.如果发现经常需要此代码,请从imline
派生一个新类.下面的示例至少显示了需要进行的操作.它仅定义一个构造函数.此示例还允许您将可选参数传递给imline
.如果未指定任何参数,则使用上面的位置值创建imline
对象.
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
示例用法:
%# Generate a 100 element array of `imline` objects,
%# but define the last one explicitly
mat(100)=myimline(gca,[0 1],[0 1]);
数组中的最后一个myimline
对象具有如分配中指定的点,但是其余元素具有如上所述的默认位置值[NaN NaN]
.
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.
这篇关于在MATLAB中存储由imline生成的对象的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!