无法在Matlab中更新类定义 [英] cannot update class definition in Matlab

查看:172
本文介绍了无法在Matlab中更新类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个令人愤怒的问题与Matlab,和一个早期的回答到显然同样的问题没有帮助我,不幸的是。我很抱歉,这个问题相当长 - 你需要相当多的信息来重现这个问题(我试着尽可能地修剪它)。

I am running into an infuriating problem with Matlab, and an earlier answer to apparently the same problem didn't help me, unfortunately. I apologize that the question is rather long - you need quite a bit of information to reproduce the problem (I tried to trim it as much as I could...)

问题是这样的:无论我做什么,我使用一个类后,我不能使Matlab忘记。所使用的值似乎是持久的,对类定义的编辑不会坚持。在后一种情况下,错误消息是:

The problem is this: No matter what I do, after I have used a class I cannot "make Matlab forget". Values used seem to be persistent, and edits to the class definition won't "stick". In the latter case, the error message is:


警告:'myClass'的类文​​件已更改;但是改变
不能应用,因为基于旧类文件的对象仍然存在
。如果使用这些对象,您可能会得到意想不到的结果。您
可以使用'clear'命令删除这些对象。有关如何删除这些对象的信息,请参阅帮助清除

Warning: The class file for 'myClass' has been changed; but the change cannot be applied because objects based on the old class file still exist. If you use those objects, you might get unexpected results. You can use the 'clear' command to remove those objects. See 'help clear' for information on how to remove those objects.

>

I get that message even after

>> clear all
>> clear functions
>> clear ans

不管怎样,类定义是持久的,尽管我试图清除它。更糟的是,当我修改类的一个实例的值,然后清除它,该值是不知何故不忘记。为了说明,这里是 myClass 的源代码:

Somehow the class definition is persistent despite my attempts to clear it. To make matters worse, when I modify a value of an instance of the class, then clear it, the value is somehow not "forgotten". To illustrate, here is the source code of myClass:

% a simple class definition that shows the problem that I cannot
% figure out how to redefine a class without restarting Matlab
classdef myClass < handle
    properties
        precursors = {'none'};
        numPre = {1};
        value = 1;
    end

    methods
        function obj = myClass(pre, num, val)
            % constructor
            if nargin > 0
                obj.precursors = pre;
                obj.numPre = num;
                obj.value = val;
            end
        end
        function v = sumVal(obj)
            % find the sum of the value of all precursors
            n = numel(obj.precursors);
            v = 0;
            for ii = 1:n
              pc = obj.precursors{ii};
              if isa(pc, 'myClass')
                  if ii==1
                      v = 0;
                  end
                  v = v + sumVal(pc) * obj.numPre{ii};
              else
                  v = obj.value;
              end
            end
        end
    end

    % only the following named instances may exist:
    enumeration
      grandpa   ({'none'},           {1},  1)
      father    ({myClass.grandpa},  {3}, -1)
      son       ({myClass.father},   {2}, -1) 
    end
end



在一个新的Matlab实例中,我执行以下操作: / p>

In a fresh instance of Matlab, I do the following:

>> son = myClass.son;
>> sumVal(son)

ans = 

     6

>> grandpa = myClass.grandpa;
>> grandpa.value = 5;
>> sumVal(son)

ans =

    30

到目前为止,这么好。 sumVal 函数发现父亲和祖父,并且正确计算 sumVal (第一种情况下为6 * 1,在第二种情况下为6 * 5)。

So far, so good. The sumVal function discovers the fathers and grandfathers, and the sumVal is computed correctly (6 * 1 in the first case, 6 * 5 in the second case).

现在我删除一切(我认为):

Now I delete "everything" (I think):

>> clear all
>> clear functions
>> clear ans

我只创建一个变量:

>> son = myClass.son;

现在是踢球 - 意想不到的回答

Now comes the kicker - the unexpected answer

>> sumVal(son)

ans =

    30

当我检查加载的变量时,我发现

When I inspect the variables loaded, I find

>> whos
Name    Size       Bytes  Class     Attributes

son      1x1         112  myClass

没有 grandpa 实例,类定义文件没有被触及。然而, grandpa (我创建,然后删除)的价值是某种程度上是持久的。

There is no grandpa instance, and the class definition file was not touched. Yet, the value of grandpa (which I created, then deleted) is somehow persistent.

myClass.m 文件进行小的更改,并尝试创建一个新变量(在清除所有之后)我得到上面显示的消息。所有这些导致我的问题:

And when I make a small change to the myClass.m file, and try to create a new variable (after a clear all), I get the message shown above. All of which leads me to my question:

Matlab隐藏我的类的实例,以便变量在后清除 code>,以及如何清除工作区(不重新启动),所以类定义是复位?

Where is Matlab hiding an instance of my class so that variables are persistent after a clear all, and how do I clear the workspace (without restarting) so the class definition is "reset"?

我不知道是否重要,但我使用Matlab 7.14.0.739(R2012a)

I don't know if it matters, but I'm using Matlab 7.14.0.739 (R2012a)

推荐答案

你有一个中间实例 myClass。父没有被MATLAB销毁。您必须自行删除

You have an intermediate instance myClass.father that is not being destroyed by MATLAB. You have to deleteit yourself

>> clear grandpa
>> delete(son.precursors{1})
>> clear son
>> clear classes
>> son = myClass.son
son = 
    son    
>> sumVal(son)
ans =
     6

编辑:
或者,您可以向类添加析构函数

Alternatively, you can add a destructor to your class

    function delete(obj)
        if isa(obj.precursors{1}, 'myClass')
            delete(obj.precursors{1});
        end
    end

并使用 delete )而不是让它清除函数来销毁。您可以将其扩展到您的案例,并递归删除树中的所有实例。

and use delete(son) instead of leaving it to clear function to destroy. You can extend this to your case and recursively delete all instances in your tree.

这篇关于无法在Matlab中更新类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆