function charging % Find a serial port object. obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', ''); % Create the serial port object if it does not exist % otherwise use the object that was found. if isempty(obj1) obj1 = serial('COM3'); else fclose(obj1); obj1 = obj1(1); end % Connect to instrument object, obj1. fopen(obj1); % Communicating with instrument object, obj1. fprintf(obj1, 'SYST:REM'); %send the remote command to the instrument fprintf(obj1, 'CURR 2'); %set current to 1A fprintf(obj1, 'VOLT 9'); %set voltage to 5V fprintf(obj1, 'OUTP ON'); %turn output on pause(1); %wait 1 second voltage = 0; while (voltage < 8.3) data1 = query(obj1, 'MEAS:VOLT?') voltage = str2double(data1) pause(1); %pause for this # of seconds end current = 1; fprintf(obj1, 'CURR 0.2'); %set current to 1 A fprintf(obj1, 'VOLT 8'); %set voltage to 8V while (current > .1) current = str2double(query(obj1, 'MEAS:CURR?')) %measure current value pause(1); end fprintf(obj1, 'OUTP OFF'); %turn output off fprintf(obj1, 'SYST:LOC'); %go into local mode % Disconnect all objects. fclose(obj1); % Clean up all objects. delete(obj1); end