MATLAB Class Object Not Updated

前端 未结 2 1932
旧巷少年郎
旧巷少年郎 2021-01-27 06:00

I am writing a simple MATLAB class that has a few properties and a method. The constructor of the class initializes the properties with the default values. The method of the cla

2条回答
  •  轮回少年
    2021-01-27 06:14

    Your class needs to inherit from the handle superclass. Try:

    classdef classTest < handle
    properties
        p1
        p2
        p3
        p4
    end
    
    methods
    
        function obj = classTest()
            obj.p1 = 0;
            obj.p2 = 0;
            obj.p3 = [];
            obj.p4 = '';
        end
    
        function obj = updateSomeProperties( obj, p1 )
            obj.p1 = p1;
        end
    end
    end 
    

    This will give,

    >> b = classTest
    
    b = 
    
    classTest with properties:
    
    p1: 0
    p2: 0
    p3: []
    p4: ''
    
    >> b.updateSomeProperties(10)
    
    ans = 
    
      classTest with properties:
    
    p1: 10
    p2: 0
    p3: []
    p4: ''
    
    >> b
    
    b = 
    
    classTest with properties:
    
    p1: 10
    p2: 0
    p3: []
    p4: ''
    

提交回复
热议问题