# wandelt ein Impedanz-Messfile in ein für LTspice anwendbares .subckt um.
# In REW kann ein .frd Messfile erstellt werden wie folgt:
# -> Export -> Export measurement as text -> 
# than: Use custom resolution: for example 96 PPO 
# © juergen@micka.de 2017


ZMA_Messfile="test"; %REW Messfile ohne .zma Endung

fid = fopen([ZMA_Messfile ".zma"]);
i=1;
j=1;
while(! feof(fid))
txt = fgetl (fid);
if (findstr(txt,"*"))
  zeilen_kommentar=j;
  kommentar(j++)={txt};
else
   zeilen_messdaten=i;
   [x(i), y(i), z(i)] = strread (txt, "%f %f %f");
   i++;
endif
endwhile
# C(i) = textscan(fid, '%s %*[^\n]');
fclose(fid);

fid = fopen ([ZMA_Messfile "_zma_ltspice.mod"], "w");
for i=1:zeilen_kommentar
  fprintf(fid,"%s\n",char(kommentar(i)))
endfor
fprintf(fid,[".subckt " ZMA_Messfile "_zma_ltspice 1 2\n"]); 
fprintf(fid,"R1 1 10 0.01\n");
fprintf(fid,"V1 10 11 0\n");
fprintf(fid,"B1 11 2 V=I(V1) MAG FREQ=\n");
for i = 1:zeilen_messdaten
  fprintf(fid ,"+(%f, %f, %f)\n",x(i),y(i),z(i));
endfor
fprintf(fid,".ends\n");
fclose (fid);



