2012-07-10 90 views
0

我很努力地理解为什么我的实例变量没有被保存。每当我改变CurrentSettings,它就不会在我下次调用另一个函数时出现。基本上它不会在每个功能后保存并恢复到0Matlab实例变量没有保存,恢复为0

classdef laserControl 
%LASERCONTROL This module is designed to control the laser unit. 
% It can set the filter position, open and close the shutter and turn 
% on/off the laser. 
% 
%%%%%%%%%%PORT LISTINGS%%%%%%%%%%% 
%The set filter command is on port0 
%The set shutter is in port1 
%Laser 1 on port2 
%Laser 2 on port3 
%The filter digits are on ports 8-15 (the are on the second box) 

properties%(GetAccess = 'public', SetAccess = 'private') 
    laserPorts; %The #'s of the output ports 
    currentSettings; %Current high/low settings 
    dio; 
end 

methods 

    %Constructor 
    %Opens the connection with the digital outputs 
    %Make sure to close the connection when finished 
    function Lobj = laserControl() 
     %Setup the laser 
     Lobj.laserPorts = [0:3 8:15];% 8:15 
     Lobj.currentSettings = zeros(1, length(Lobj.laserPorts)); 
     %Make connection and reset values 
     Lobj.dio = digitalio('nidaq','Dev1'); 
     addline(Lobj.dio, Lobj.laserPorts, 'out'); 
     putvalue(Lobj.dio, Lobj.currentSettings); 
    end 

    %Closes the connection to the digital output 
    function obj = CloseConnection(obj) 
     putvalue(obj.dio, zeros(1, length(obj.currentSettings))); 
     delete(obj.dio); 
     clear obj.dio; 
    end 


    %Sets the position of the filter. 
    %positionValue - the integer amount for the position, cannot be 
    %larger than 150, as regulated by the box. 
    %The set filter command is on port0 
    %The filter digits are on ports 8-15 (the are on the second box) 
    function obj = SetFilterPosition(obj, positionValue) 
     if 0 <= positionValue && positionValue < 150 
      binaryDigit = de2bi(positionValue); %Convert it to binary form 
      %LaserOn OldSettings NewValue ExtraZeros 
      obj() 
      obj.currentSettings() 
      obj.currentSettings = [1 obj.currentSettings(1, 2:4) binaryDigit... 
       zeros(1, 8 - length(binaryDigit))]; 
      putvalue(obj.dio, obj.currentSettings); 
     else 
      display('Error setting the filer: Value invalid'); 
     end 
    end 
end 
+1

也许你还应该张贴描述你的工作流的代码,即你如何实例化对象(S),你调用哪个方法。 – georg 2012-07-10 21:18:24

回答

1

因为你的类不从handle继承,你已经写了一个“价值”型类 - 换句话说,当你做出改变,你必须捕捉的返回值,就像这样:

myObj = SetFilterPosition(myObj, 7); 

更多关于手柄和值类,见the doc