%get and plot IV curve;
%Last step sets voltage to 0.
clear all
compliance = 10e-3; %A
Vstart = -5;         %V
Vstop = 2;         %V
Vstep = 0.1;       %V
pse = .1;         %How long to pause in between points (sec)
Vvec_up = Vstart:Vstep:Vstop;
Vvec_down = Vstop:-Vstep:Vstart;
meterGPIB = 26;

%initialize source meter
address = strcat('GPIB0::', num2str(meterGPIB), '::INSTR');
source = visa('ni', address);
fopen(source)
voltage = Vvec_up(1);
fprintf(source, 'smua.source.func=smua.OUTPUT_DCVOLTS');
fprintf(source, 'smua.source.levelv = %e', voltage);
fprintf(source, 'smua.source.output = smua.OUTPUT_ON');
fprintf(source, 'curr=smua.measure.i()');
fprintf(source, 'print(curr)');
current = str2num(fscanf(source));

%step through voltages: go up:
for i = 1:length(Vvec_up)
    voltage = Vvec_up(i);
    fprintf(source, 'smua.source.levelv = %e', voltage);
    pause(pse);
    fprintf(source, 'curr=smua.measure.i()');
    fprintf(source, 'print(curr)');
    current_up(i) = str2num(fscanf(source));
end

plot(Vvec_up, current_up)

%step through voltages: go down:
for i = 1:length(Vvec_down)
    voltage = Vvec_down(i);
    fprintf(source, 'smua.source.levelv = %e', voltage);
    fprintf(source, 'curr=smua.measure.i()');
    fprintf(source, 'print(curr)');
    current_down(i) = str2num(fscanf(source));
    pause(pse);
end

fprintf(source, 'smua.source.levelv = 0');
fclose(source)
clear source

plot(Vvec_up, current_up*1e3, Vvec_down, current_down*1e3)
ylabel('Current (mA)')
xlabel('Voltage (V)')
ylim([-1*compliance*1e3 compliance*1e3])

%uisave()

    

