2016-05-17 95 views
-1
classdef hello < handle 
     methods 
      function first(obj) 
       a=4; 
       function second(obj) 
        a 
       end 
      end 
     end 
end 

我希望能够调用函数“obj.second”。调用类中的嵌套函数

这是我曾尝试:

>> hello_obj=hello; 
>> hello_obj.first 
>> hello_obj.second 
No appropriate method, property, or field second for class hello. 

>> hello_obj.first.second 
Attempt to reference field of non-structure array. 

>> hello_obj.first.hello_obj.second 
Attempt to reference field of non-structure array. 

谢谢

+0

这是不可能的,你为什么认为你必须这样做? – Daniel

+0

我想利用嵌套函数可以访问父函数的工作空间的事实(我不希望“a”是属性)。 –

+0

当你尝试调用'second'时,'a'不再存在。我认为一个财产将是最好的解决方案,但你可以解释你的理由不使用财产。这可能会导致更好的解决方案。 – Daniel

回答

1

使用Transient属性a保存时跳过它。对于无法应用Transistent的情况,重载saveobj and loadobj是一个很好的选择。

+0

你能帮我使用它吗?抱歉。 classdef hello properties a = 4 end properties(Transient = true)b = 23 end end >> hello_obj = hello; >> save('hello_obj')hello_obj.mat包含一个包含两个属性的对象。 –

+0

@Michaël:不,它不会将'b'存储在mat文件中,它会使用将其设置为23的构造函数重新初始化。尝试'hello_obj = hello; hello_obj.a = 5; hello_obj.b = 24; save('hello_obj'); clear hello_obj; 加载('hello_obj')' – Daniel

+0

谢谢!实际上,我不希望hello_obj.b被保存,甚至作为一个空变量。 –