2008-11-07 2 views
21

매트랩 개체의 속성, 뭔가를 수정하는 방법 :같은 내가 MATLAB 클래스를 생성 한

classdef myclass 

    properties 
     x_array = []; 
    end 

    methods 
    function increment(obj,value) 
     obj.x_array = [obj.x_array ; value); 
    end 
    end 
end 

나는 increment() 함수를 호출 할 때 문제가 있으며,이 건물 x_array이 수정되지 않습니다되어 예 :

>>s = myclass 
>>increment(s,5) 

>>s.x_array 
ans = [] 

나는 몇 가지 조사를했고, 아무도 이유를 알고 않고,이 때문에 MATLAB 내 클래스는 HANDLE 클래스는이 문제를 해결해야 상속하고, 개체에 대한 게으른 복사를 사용하는하다는 결론에 도달하지만,하지 않았다 이게 다행이다. 닝? 그리고 핸들 클래스를 확장하는 것이 실제로 해결책 일 경우 올바른 방법이 아닌가요?

classdef myclass < handle 

또는 추가 단계가 있습니까?

+0

http://stackoverflow.com/questions/209005/object-oriented-matlab-properties와 거의 중복됩니다. – Azim

답변

22

이것은 this question과 유사합니다. 간단히 말해서 핸들 클래스에서 상속 받아야 할 모든 것입니다. Matlab의 명령 프롬프트에서 이제 파일 myclass.m

classdef myclass<handle 
    properties 
     x_array = [] 
    end 
    methods 
     function obj=increment(obj,val) 
      obj.x_array=[obj.x_array val]; 
     end 
    end 
end 

빠른 예를

내용은 다음

>> s=myclass; 
>> s.increment(5) 
>> s.increment(6) 
>> s 

s = 

myclass handle 

properties: 
    x_array: [5 6] 

lists of methods, events, superclasses 
-1

가 더 쉬운 방법이있다 할 수 있습니다.

s= s.x_array 
여기

더 많은 정보 : : http://uk.mathworks.com/help/matlab/matlab_oop/matlab-vs-other-oo-languages.html#bslvcv1

PS :이 핸들를 사용하는 미세하지만, 방법 복사 기능이 작동하면 다른 다음과 같이 당신은 당신의 초기 예를 s을 덮어 필요 당신이 그것을 사용하는 방법에주의해야합니다. 핸들을 사용하는 경우 실제로는 obj에 대한 새 주소/참조를 만들고 있습니다

관련 문제