在 MATLAB 中存储 imline 生成的对象的句柄

发布于 2024-11-26 03:33:40 字数 714 浏览 2 评论 0原文

我试图在数组中存储一组对象句柄。这些对象是由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自控 2024-12-03 03:33:40

使用 empty 静态方法创建类类型的空数组:

lines = imline.empty(0,10);
for idx=1:10
    line = imline(gca, sortrows(rand(2,2)));
    set(line,'UserData',idx)
    lines(idx) = line;
end

在此处输入图像描述

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

叶落知秋 2024-12-03 03:33:40

您可能需要使用默认值行填充矩阵才能创建它。预分配大小为 N 的对象矩阵的典型方法是简单地将对象分配给矩阵中的最后一个元素。

M(N,N)=imline(gca,[NaN NaN],[NaN NaN]); %# set non-displayable vals for x and y

注意,上面的行不适用于 imline,因为它将为矩阵中的每个其他 N*N-1 imline 对象调用默认构造函数,并调用不带参数的 imline 强制用户与当前轴交互。

我的建议(如果您要预先分配)是在矩阵中显式定义所有默认行:

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 对象。

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]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文