2012-07-10 88 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