Matlab OOP:如何更新对象的属性

Matlab OOP: How to update properties of an object

classdef PortfolioX 
       properties 
          name; 
          anzahl=0; 
          myValue=0; 
          handeln; 
          preis=0; 
       end 

  function [this, portfolio1] = setAnzahl(this, portfolio1, AktienAnzahl, AktienPreis) 
  .... 
  this.preis = AktienPreis; 
  portfolio1.value = AktienAnzahl;
   ... 
   end
end



function StartTrading(DDD, Portfolio) 
%if text.mat 
load test.mat 
%end 

%Aktie = DDD; 
DDD.handeln = 'buy'; 
    %Anzahl an Aktien die gehandelt werden und deren aktueller Preis 
    [DDD, Portfolio] = setAnzahl(DDD, Portfolio, 200, 5); 

    save test.mat 
load test.mat 

现在我执行以下操作:

  1. 构造对象 DDD 和对象组合
  2. 我开始StartTrading(DDD, Portfolio)
  3. 结果:对象 DDD 和 Portfolio 的属性值与 建造。

我期望的是,他们更新了值(preis = 50 和 handeln = 'buy')。 我的错误是什么?谢谢!

您已经在 MATLAB 中创建了一个值对象。当您将这些对象传递给函数时,会为它们创建一个副本。这些副本由您在函数内部修改。除非你return把这些拷贝回来,否则你只有函数外未修改的原始对象。

您可以 return 函数中的对象并将它们分配回相同的变量,或者通过从句柄继承来使用句柄对象。请参阅 http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html

处比较句柄和值 class 的文档