Matlab实例变量未保存,恢复为0

时间:2012-07-10 20:47:18

标签: oop class matlab instance

我正在努力理解为什么我的实例变量没有被保存。每当我对CurrentSettings进行更改时,下次调用另一个函数时都不会出现。基本上它不会在每个函数后保存并恢复为0 s。

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 个答案:

答案 0 :(得分:1)

因为你的类没有从handle继承,所以你写了一个“值”类型 - 换句话说,当你进行更改时,你必须捕获返回值,如下所示:

myObj = SetFilterPosition( myObj, 7 );

有关句柄和值类的更多信息,请参阅the doc