% Author: C. Talarico % File name: ltsExp2Matlab.m % ltsExp2Matlab.m reads an LTspice sim. exported as textfile into Matlab % This version of the function supports DC sweep analysis, TRAN analysis % and AC analysis % % Calling Convention: % [nvars blocks points PDATA] = ltsExp2Matlab( FILENAME ) % % nvars - number of sim. variables plotted % blocks - number of sim. "steps" (number of data sets = blocks*nvars) % points - number of points in each data set % PDATA - processed data (mR x nR Matrix) % mR = blocks*nvars, nR = points function [nvars blocks points PDATA] = ltsExp2Matlab( FILENAME ) format LONGG; countsteps = 0; countpoints = 0; fid = fopen(FILENAME); tline = fgetl(fid); fline = tline; % keep the first line while isstr(tline) S = tline; [num,ok] = str2num(S); if (ok == 0) R = regexp(S,'Step','match'); if (strcmp(R,'Step')) countsteps = countsteps + 1; disp(tline); end end if (ok == 1) countpoints = countpoints + 1; % NEED TO PARSE THE STRING INTO ITS FIELDS THEN VECTORIZE THE FIELDS str = S; A=strread(str,'%s'); % default delimiter: one or more spaces [mA nA] = size(A); % m is the number of variables we observe % check if the variables are real numbers or complex numbers % NOTE: str2num has issues, use str2double instead for l=1:mA a = char(A(l)); % need a type casting occ = findstr(a,','); % real and imag part are separated by a comma if (isempty(occ) == 1) % it is a real number data(countpoints,l) = str2double(a); else % it is a complex number cnum = strsplit(a,','); % complex number string re = str2double(cnum(1)); im = str2double(cnum(2)); c = complex(re,im); data(countpoints,l) = c; end end end tline = fgetl(fid); end if countsteps == 0 blocks = 1; else blocks = countsteps; end fclose(fid); nvars = mA; % number of variables observed totpoints = countpoints; % total number of data points per variable points = totpoints/blocks; % number of points per step blocks; % number of steps s1 = ['number of variables plotted is ', num2str(nvars), ':']; fprintf('\n%s \t %s \n\n', s1, fline); s2 = ['total data points: ', num2str(totpoints), ' x ', num2str(nvars)]; fprintf('%s \t\t\t \n\n', s2); s3 = ['simulation steps: ', num2str(blocks)]; fprintf('%s \t\t\t \n\n', s3); s4 = ['data points per simulation step: ', num2str(points), ' x ', ... num2str(nvars)]; fprintf('%s \t\t\t \n\n', s4); tdata = transpose(data); tdatasize = size(tdata); % totpoints x nvars PDATA = zeros(blocks*nvars,points); [mR nR] = size(PDATA); vindex = 0; % nvars's index counter for i=1:mR bindex = ceil(i/nvars); % block's index vindex = vindex + 1; if vindex > nvars vindex = 1; % reset the vindex counter end % vindex % 1+points*(bindex-1) % points*bindex PDATA(i,:) = tdata(vindex,1+points*(bindex-1):points*bindex); tdata(vindex,1+points*(bindex-1):points*bindex); end % PDATA