function outputStruct = set(obj, varargin)
%SET Configure or display serial port object properties.
%
%    SET(OBJ,'PropertyName',PropertyValue) sets the value, PropertyValue,
%    of the specified property, PropertyName, for serial port object OBJ.
%
%    OBJ can be a vector of serial port objects, in which case SET sets 
%    the property values for all the serial port objects specified.
%
%    SET(OBJ,S) where S is a structure whose field names are object property 
%    names, sets the properties named in each field name with the values 
%    contained in the structure.
%
%    SET(OBJ,PN,PV) sets the properties specified in the cell array of
%    strings, PN, to the corresponding values in the cell array PV for all
%    objects specified in OBJ. The cell array PN must be a vector, but the 
%    cell array PV can be M-by-N where M is equal to length(OBJ) and N is
%    equal to length(PN) so that each object will be updated with a different
%    set of values for the list of property names contained in PN.
%
%    SET(OBJ,'PropertyName1',PropertyValue1,'PropertyName2',PropertyValue2,...)
%    sets multiple property values with a single statement. Note that it
%    is permissible to use param-value string pairs, structures, and
%    param-value cell array pairs in the same call to SET.
%
%    SET(OBJ, 'PropertyName') 
%    PROP = SET(OBJ,'PropertyName') displays or returns the possible values
%    for the specified property, PropertyName, of serial port object OBJ. 
%    The returned array, PROP, is a cell array of possible value strings  
%    or an empty cell array if the property does not have a finite set of
%    possible string values.
%   
%    SET(OBJ) 
%    PROP = SET(OBJ) displays or returns all property names and their
%    possible values for instrument object OBJ. The return value, PROP, is
%    a structure whose field names are the property names of OBJ, and whose 
%    values are cell arrays of possible property values or empty cell arrays.
%
%    Example:
%       s = serial('COM1');
%       set(s, 'BaudRate', 9600, 'Parity', 'even');
%       set(s, {'StopBits', 'RecordName'}, {2, 'sydney.txt'});
%       set(s, 'Name', 'MySerialObject');
%       set(s, 'Parity')
%
%    See also SERIAL/GET.
%

%    MP 7-13-99
%    Copyright 1999-2000 The MathWorks, Inc. 
%    $Revision: 1.5 $  $Date: 2000/09/01 16:17:30 $

% Call builtin set if OBJ isn't an instrument object.
% Ex. set(s, 'UserData', s);
if ~isa(obj, 'instrument')
    try
	    builtin('set', obj, varargin{:});
    catch
        error(lasterr);
    end
    return;
end

% Error if invalid.
if ~all(isvalid(obj))
   error('Instrument object OBJ is an invalid object.');
end

if (nargout == 0)
   % Ex. set(obj)
   if nargin == 1
      if (length(obj) == 1)
         localCreateSetDisplay(obj);
         return;
      else
         error('Vector of handles not permitted for set(OBJ).');
      end
   else
      % Ex. set(obj, 'BaudRate');
      % Ex. set(obj, 'BaudRate', 4800);
      try
         % Call the java set method.
         if (nargin == 2)
            if isstr(varargin{1}) 
                % Ex. set(obj, 'RecordMode')
				disp(char(createPropSetDisplay(igetfield(obj, 'jobject'), varargin(1), localFindType(obj))));
            else
                % Ex. set(obj, struct);
                tempObj = igetfield(obj, 'jobject');
            	tempObj.set(varargin{:});
            end
         else
            % Ex. set(obj, 'BaudRate', 4800); 
            tempObj = igetfield(obj, 'jobject');
            tempObj.set(varargin{:});
         end
      catch
         localFixError;
         error(lasterr);
      end	
   end
else
   % Ex. out = set(obj);
   % Ex. out = set(obj, 'BaudRate');
   try
      % Call the java set method.
	  switch nargin 
	  case 1
          % Ex. out = set(obj);
       	  outputStruct = set(igetfield(obj, 'jobject'), varargin{:});
	  case 2
		  % Ex. out = set(obj, 'BaudRate')
		  if ~isstr(varargin{1})
			  % Ex. out = set(obj, {'BaudRate', 'Parity'});
			  error('Invalid parameter/value pair arguments.');
		  end
		  
		  outputStruct = createPropSetArray(igetfield(obj, 'jobject'), varargin{1}, localFindType(obj));
		  if isempty(outputStruct)
			  outputStruct = {};
		  end
		  		  
	  case 3
		  % Ex. out = set(obj, 'BaudRate', 9600)
          set(igetfield(obj, 'jobject'), varargin{:});
      end
   catch
      localFixError;
      error(lasterr);
   end	
end	

% **********************************************************************
% Create the display for SET(OBJ)
function localCreateSetDisplay(obj)

fprintf(char(setDisplay(igetfield(obj, 'jobject'))));

% *******************************************************************
% Get the type of object for the error message.
function type = localFindType(obj)

type = igetfield(obj, 'type');
switch type
case 'serial'
	type = 'serial port';
case 'visa-serial'
	type = 'VISA-serial'
otherwise
	type = upper(type);
end

% *******************************************************************
% Fix the display.
function [out, errflag] = localFixDisplay(obj, str, prop)

errflag = 0;
out = str;

if (findstr('com.mathworks.toolbox.instrument.', str))
    out = strrep(str, 'com.mathworks.toolbox.instrument.', '');
end

if findstr('SerialComm', out)
   out = strrep(out, 'SerialComm', 'serial port');
elseif findstr('SerialVisa', out)
   out = strrep(out, 'SerialVisa', 'VISA-Serial');
elseif findstr('GpibDll', out)
   out = strrep(out, 'GpibDll', 'GPIB');
elseif findstr('GpibVisa', out)
   out = strrep(out, 'GpibVisa', 'VISA-GPIB');
elseif findstr('VxiVisa', out)
   out = strrep(out, 'VxiVisa', 'VISA-VXI');
elseif findstr('VxiGpibVisa', out)
    out = strrep(out, 'VxiGpibVisa', 'VISA-GPIB-VXI');
elseif findstr('Gpib', out)
   index = findstr('Gpib', out);
   blanks = findstr(out, ' ');
   endloc = find(blanks > index);
   endloc = blanks(endloc(1));
   out = strrep(out, out(index:endloc-1), 'GPIB');
end

% *******************************************************************
% Fix the error message.
function localFixError

% Initialize variables.
out = lasterr;

if localfindstr('com.mathworks.toolbox.instrument.', out)
    out = strrep(out, sprintf('com.mathworks.toolbox.instrument.'), '');
end

if localfindstr('javahandle.', out)
	out = strrep(out, sprintf('javahandle.'), '');
end

if localfindstr('SerialComm', out)
   out = localstrrep(out, 'SerialComm', 'serial port objects');
   out = localstrrep(out, 'in the ''serial port objects'' class', 'for serial port objects');
elseif localfindstr('SerialVisa', out)
   out = localstrrep(out, 'SerialVisa', 'VISA-serial objects');
   out = localstrrep(out, 'in the ''VISA-serial objects'' class', 'for VISA-serial objects');
elseif localfindstr('GpibDll', out)
   out = localstrrep(out, 'GpibDll', 'GPIB objects');
   out = localstrrep(out, 'in the ''GPIB objects'' class', 'for GPIB objects');
elseif localfindstr('VxiGpibVisa', out)
    out = localstrrep(out, 'VxiGpibVisa', 'VISA-GPIB-VXI objects');
    out = localstrrep(out, 'in the ''VISA-GPIB-VXI objects'' class', 'for VISA-GPIB-VXI objects');
elseif localfindstr('GpibVisa', out)
   out = localstrrep(out, 'GpibVisa', 'VISA-GPIB objects');
   out = localstrrep(out, 'in the ''VISA-GPIB objects'' class', 'for VISA-GPIB objects');
elseif localfindstr('VxiVisa', out)
   out = localstrrep(out, 'VxiVisa', 'VISA-VXI objects');
   out = localstrrep(out, 'in the ''VISA-VXI objects'' class', 'for VISA-VXI objects');
elseif localfindstr('Gpib', out)
   index = localfindstr('Gpib', out);
   blanks = findstr(out, ' ');
   endloc = find(blanks > index);
   endloc = blanks(endloc(1));
   out = localstrrep(out, out(index:endloc), 'GPIB objects ');
   out = localstrrep(out, 'in the ''GPIB objects class', 'for GPIB objects');
end

% Remove the trailing carriage returns from errmsg.
while out(end) == sprintf('\n')
   out = out(1:end-1);
end

lasterr(out);

% *******************************************************************
% findstr which handles possible japanese translation.
function result = localfindstr(str1, out)

result = findstr(sprintf(str1), out);

% *******************************************************************
% strrep which handles possible japanese translation.
function out = localstrrep(out, str1, str2)

out = strrep(out, sprintf(str1), sprintf(str2));
