function delete(obj)
%DELETE Remove serial port object from memory.
%
%    DELETE(OBJ) removes serial port object, OBJ, from memory. When OBJ
%    is deleted, it becomes an invalid object and cannot be reconnected 
%    to the device and should be removed from the workspace with CLEAR.
%
%    If multiple references to a serial port object exist in the workspace,
%    then deleting one serial port object invalidates the remaining 
%    references. These remaining references should be cleared from the
%    workspace with the CLEAR command.
%
%    An error will be returned if you delete a serial port object that 
%    is connected to the device, i.e., has a Status property value of 
%    open. A serial port object can be disconnected from the device with
%    the FCLOSE function.    
% 
%    An error will be returned if you delete a serial port object while
%    data is being written asynchronously to the device. The STOPASYNC
%    function can be used to stop an asynchronous write.
% 
%    If OBJ is an array of serial port objects and one of the objects
%    cannot be deleted, the remaining objects in the array will be deleted 
%    and a warning will be displayed.
%
%    DELETE should be used at the end of a serial port session.
%
%    See also SERIAL/FCLOSE, SERIAL/STOPASYNC, SERIAL/ISVALID.
%

%    MP 7-13-99
%    Copyright 1999-2000 The MathWorks, Inc. 
%    $Revision: 1.4 $  $Date: 2000/08/29 23:47:28 $

% Initialize variables.
errorOccurred = 0;
jobject = igetfield(obj, 'jobject');

% Call dispose on each java object.  Keep looping even 
% if one of the objects could not be deleted.  
for i=1:length(jobject)
   try
      dispose(jobject(i));
      disconnect(jobject(i));
  catch
   	  errorOccurred = 1;	    
   end   
end   

% Report error if one occurred.
if errorOccurred
    if length(jobject) == 1
   		error(lasterr);
    else
        warnState = warning;
        if ~strcmp(warnState, 'off')
            warning('on');
        end
        warning('An object in OBJ could not be deleted.');
        warning(warnState);
    end
end
