2013-03-16 43 views
3

我在MATLAB中有以下目录结构的项目:你如何使用matlab中的对象作为成员变量来创建类?

+namespace\ 
    @class1\ 
     class1.m 
    @class2\ 
     class2.m 
mainfile.m 

在class1.m我有类似如下的

classdef class1 

    %readonly variables 
    properties(GetAccess = 'public',SetAccess = 'private') 
     forename; 
     lastname; 
     middlename; 

    end 

    properties(Constant = true) 

     %in centipascals 
     p1 = class2(param1,param2); %this is the part I need to work 

    end 

    methods(Access = public) 

     function this = class1(fname,lname,mname) 

      this.forename = fname; 
      this.lastname = lname; 
      this.middlename = mname; 

     end 
    end 
end 

我似乎无法得到这个一流的工作。 Class1不能识别class2的构造函数(可能是因为某些东西没有被正确导入)。如何导入class2或为了将其他类实例作为成员变量而需要做什么?

+0

您需要将这些目录添加到路径。文件>设置路径>添加文件夹或使用添加路径功能。 – Justin 2013-03-16 19:07:04

回答

0

在Matlab中,您需要完全限定对名称空间中的类的引用,即使是来自同一名称空间内的其他类也是如此。喜欢这个。

classdef class1 
    properties (Constant = true) 
     %in centipascals 
     p1 = namespace.class2(param1,param2); 
    end 
end 

您可以import其他类来自同一个命名空间,但是import s的每个功能级别只工作,不以性能块在所有的工作据我所知,这样就不会在这个特定的工作的情况下,可能会比其他地方的价值更麻烦。

+0

谢谢!当我将它导入它的功能块时,它就工作了 – thed0ctor 2013-03-18 01:28:48

相关问题