function fclose(obj)
%FCLOSE Disconnect serial port object from device.
%
%    FCLOSE(OBJ) disconnects the serial port object, OBJ, from the 
%    device. OBJ can be an array of serial port objects.
%
%    If OBJ was successfully disconnected from the device, OBJ's
%    Status property is configured to closed and the RecordStatus 
%    property is configured to off. OBJ can be reconnected to the 
%    device with the FOPEN function.
%
%    You cannot disconnect an 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 disconnected from the device, the remaining objects  
%    in the array will be disconnected from the device and a warning
%    will be displayed.
%
%    Example:
%       s = serial('COM1');
%       fopen(s);
%       fprintf(s, '*IDN?');
%       idn = fscanf(s);
%       fclose(s);
%
%    See also SERIAL/FOPEN, SERIAL/STOPASYNC.
%

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

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

% Call fclose on each java object.  Keep looping even 
% if one of the objects could not be closed.  
for i=1:length(jobject),
   try
      fclose(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 closed or is invalid.');
        warning(warnState);
    end
end

