%get data from Agilent 8703A
clear all
Addr =16;


address = strcat('GPIB0::', num2str(Addr), '::INSTR');
%LCA = visa('ni', address);
LCA=gpib('ni', 0, Addr);
fopen(LCA)
fprintf(LCA, '*IDN?')
Identity = fscanf(LCA);
warning('off', 'instrument:fscanf:unsuccessfulRead')
fprintf(LCA, 'POIN?')
numpoints = str2num(fscanf(LCA));
fprintf(LCA, 'STAR?')
startF = str2num(fscanf(LCA));
fprintf(LCA, 'SPAN?');
span = str2num(fscanf(LCA));
freq = linspace(startF, (startF+span), numpoints);
%numpoints=801;
%go_till = ceil(numpoints/(10))-1;       %for 401 points
go_till = ceil(numpoints/(10))-2;       %for 801 points

fprintf(LCA, 'form4')
fprintf(LCA, 'outpform')
holder=fscanf(LCA);
trace = '';
while length(holder)>511
    trace=strcat(trace, holder);
    holder = fscanf(LCA);
end
trace=strcat(trace, holder);

fclose(LCA)
clear LCA

% for j=1:(length(trace)-3);
%     if [trace(j) trace(j+1)]==',0'
%         %disp(',')
%         trace = [trace(1:j) '   ' trace(j+1:length(trace))];
%     end
%     if [trace(j) trace(j+1) trace(j+2)]==', 0'
%         %disp(',space')
%         trace = [trace(1:j) '  ' trace(j+1:length(trace))];
%     end
%     if [trace(j) trace(j+1) trace(j+2) trace(j+3)]==',  0'
%         %disp(',2xspace')
%         trace = [trace(1:j) ' ' trace(j+1:length(trace))];
%     end
%     if [trace(j) trace(j+1)]=='0-'
%         %disp('0-')
%         trace =[trace(1:j) sprintf('\n') '  ' trace(j+1:length(trace))];
%     end
%     if [trace(j) trace(j+1) trace(j+2)]=='0 -'
%         %disp('0 -')
%         trace =[trace(1:j) sprintf('\n') '  ' trace(j+2:length(trace))];
%     end
%     if [trace(j) trace(j+1) trace(j+2) trace(j+3)]=='0  -'
%         %disp('0  -')
%         trace =[trace(1:j) sprintf('\n') '  ' trace(j+3:length(trace))];
%     end
% end
% holder2 = str2num(trace);
% tracedata = holder2(:,1);
% 
% plot(freq, tracedata);
% xlabel('Frequency (Hz)');
% ylabel('Magnitude (dB)');


% uisave();
