Azzera filtri
Azzera filtri

Function file doesn`t work

21 visualizzazioni (ultimi 30 giorni)
Alisa-Oleksandra Kotliarova
Alisa-Oleksandra Kotliarova il 11 Lug 2024 alle 17:19
Risposto: Voss il 11 Lug 2024 alle 18:34
Hello everyone,
I have three files: a txt. notebook with data, an .m-function file that should read it und the final .m-file that should put out a Bode-Diagramm.
M=readLTspice('Lab5_R_100_MOhm.txt', 'Nyquist')
M = 401x1 cell array
{'1.00000000000000e+01, -2.08189672696186e+01 4.09652407317561e+00 , -2.08282439196099e+01 2.30078164043620e+00 , -2.08289762322040e+01 5.01660348954929e-01 ' } {'1.02329299228075e+01, -2.08181283142261e+01 4.15357406497834e+00 , -2.08280079148297e+01 2.31618666349063e+00 , -2.08289420000263e+01 4.75171371133909e-01 ' } {'1.04712854805090e+01, -2.08172537426266e+01 4.21280474346613e+00 , -2.08277645633479e+01 2.33281685928302e+00 , -2.08289099234277e+01 4.48934122648612e-01 ' } {'1.07151930523761e+01, -2.08163417223260e+01 4.27424600877758e+00 , -2.08275133504514e+01 2.35068086982330e+00 , -2.08288799344165e+01 4.22934708601109e-01 ' } {'1.09647819614319e+01, -2.08153903435541e+01 4.33792882437890e+00 , -2.08272537448840e+01 2.36978797667213e+00 , -2.08288519694245e+01 3.97159359259556e-01 ' } {'1.12201845430196e+01, -2.08143976154200e+01 4.40388522596117e+00 , -2.08269851977339e+01 2.39014810496063e+00 , -2.08288259691729e+01 3.71594422831076e-01 ' } {'1.14815362149688e+01, -2.08133614619167e+01 4.47214833037342e+00 , -2.08267071412837e+01 2.41177182767898e+00 , -2.08288018785463e+01 3.46226358293909e-01 ' } {'1.17489755493953e+01, -2.08122797177649e+01 4.54275234456042e+00 , -2.08264189878222e+01 2.43467037022449e+00 , -2.08287796464768e+01 3.21041728283095e-01 ' } {'1.20226443461741e+01, -2.08111501240923e+01 4.61573257447091e+00 , -2.08261201284158e+01 2.45885561522137e+00 , -2.08287592258354e+01 2.96027192028268e-01 ' } {'1.23026877081238e+01, -2.08099703239392e+01 4.69112543390032e+00 , -2.08258099316357e+01 2.48434010759362e+00 , -2.08287405733323e+01 2.71169498340568e-01 ' } {'1.25892541179417e+01, -2.08087378575848e+01 4.76896845323113e+00 , -2.08254877422401e+01 2.51113705989832e+00 , -2.08287236494257e+01 2.46455478642213e-01 ' } {'1.28824955169313e+01, -2.08074501576858e+01 4.84930028802747e+00 , -2.08251528798069e+01 2.53926035791243e+00 , -2.08287084182377e+01 2.21872040039108e-01 ' } {'1.31825673855641e+01, -2.08061045442210e+01 4.93216072743863e+00 , -2.08248046373158e+01 2.56872456647044e+00 , -2.08286948474786e+01 1.97406158429501e-01 ' } {'1.34896288259165e+01, -2.08046982192330e+01 5.01759070236573e+00 , -2.08244422796765e+01 2.59954493554465e+00 , -2.08286829083784e+01 1.73044871648373e-01 ' } {'1.38038426460288e+01, -2.08032282613610e+01 5.10563229333777e+00 , -2.08240650421994e+01 2.63173740657307e+00 , -2.08286725756261e+01 1.48775272642225e-01 ' } {'1.41253754462275e+01, -2.08016916201548e+01 5.19632873804297e+00 , -2.08236721290064e+01 2.66531861901755e+00 , -2.08286638273158e+01 1.24584502671697e-01 ' } {'1.44543977074593e+01, -2.08000851101640e+01 5.28972443845615e+00 , -2.08232627113798e+01 2.70030591715966e+00 , -2.08286566449008e+01 1.00459744537583e-01 ' } {'1.47910838816821e+01, -2.07984054047934e+01 5.38586496749805e+00 , -2.08228359260431e+01 2.73671735711753e+00 , -2.08286510131539e+01 7.63882158289763e-02 ' } {'1.51356124843621e+01, -2.07966490299161e+01 5.48479707515860e+00 , -2.08223908733742e+01 2.77457171408543e+00 , -2.08286469201356e+01 5.23571621875418e-02 ' } {'1.54881661891248e+01, -2.07948123572372e+01 5.58656869401342e+00 , -2.08219266155444e+01 2.81388848978482e+00 , -2.08286443571681e+01 2.83538505867324e-02 ' } {'1.58489319246111e+01, -2.07928915973989e+01 5.69122894405187e+00 , -2.08214421745813e+01 2.85468792011740e+00 , -2.08286433188180e+01 4.36556262132412e-03 ' } {'1.62181009735893e+01, -2.07908827928189e+01 5.79882813673923e+00 , -2.08209365303513e+01 2.89699098302055e+00 , -2.08286438028838e+01 -1.96204121956790e-02 '} {'1.65958690743756e+01, -2.07887818102543e+01 5.90941777821851e+00 , -2.08204086184589e+01 2.94081940650403e+00 , -2.08286458103917e+01 -4.36167831323406e-02 '} {'1.69824365246174e+01, -2.07865843330826e+01 6.02305057156269e+00 , -2.08198573280569e+01 2.98619567686873e+00 , -2.08286493455978e+01 -6.76362649355398e-02 '} {'1.73780082874937e+01, -2.07842858532911e+01 6.13978041797301e+00 , -2.08192814995658e+01 3.03314304708767e+00 , -2.08286544159970e+01 -9.16915845331655e-02 '} {'1.77827941003892e+01, -2.07818816631675e+01 6.25966241681895e+00 , -2.08186799222959e+01 3.08168554534757e+00 , -2.08286610323390e+01 -1.15795487740835e-01 '} {'1.81970085860998e+01, -2.07793668466832e+01 6.38275286440308e+00 , -2.08180513319698e+01 3.13184798372762e+00 , -2.08286692086508e+01 -1.39960745978628e-01 '} {'1.86208713666287e+01, -2.07767362705622e+01 6.50910925133189e+00 , -2.08173944081392e+01 3.18365596700682e+00 , -2.08286789622665e+01 -1.64200163001421e-01 '} {'1.90546071796325e+01, -2.07739845750274e+01 6.63879025836344e+00 , -2.08167077714922e+01 3.23713590158876e+00 , -2.08286903138642e+01 -1.88526581643838e-01 '} {'1.94984459975804e+01, -2.07711061642184e+01 6.77185575059345e+00 , -2.08159899810462e+01 3.29231500451642e+00 , -2.08287032875095e+01 -2.12952890586412e-01 '}
f=M(:,1);
A=M(:,2);
Index in position 2 exceeds array bounds. Index must not exceed 1.
Ph=M(:,3);
figure(1)
subplot(2,1,1);
plot(f, A , "b");
set(gca, 'XScale', 'log');
subplot(2,1,2);
plot(f, Ph, "g");
set(gca, 'XScale', 'log');
function [M]=readLTspice(fileName, selector)
% MATLAB function to read files exported from LTspice
% M = readLTspice(fileName, selector)
% fileName - name of file to be read as string (including .txt)
% selector - type of simulation (Time, Bode or Nyquist)
% data in time or cartesian form
if selector=="Time" | selector=="time" | selector=="Nyquist" | selector=="nyquist"
tempFileName = strcat(erase(fileName, '.txt'), '_Temp.txt');
textOriginal = readTFile(fileName);
textKorrigiert = regexprep(textOriginal(3:end), '\t', ',');
writeTFile(tempFileName,textKorrigiert);
M = importdata(tempFileName);
delete(tempFileName);
% data in polar form
elseif selector=="Bode" | selector=="bode"
tempFileName = strcat(erase(fileName, '.txt'), '_Temp.txt');
textOriginal = readTFile(fileName);
textKorrigiert = regexprep(textOriginal(3:end), '\t', ',');
textKorrigiert = regexprep(textKorrigiert, '°', '');
textKorrigiert = regexprep(textKorrigiert, 'dB', '');
textKorrigiert = regexprep(textKorrigiert, '(', '');
textKorrigiert = regexprep(textKorrigiert, ')', '');
writeTFile(tempFileName,textKorrigiert);
M = importdata(tempFileName);
delete(tempFileName);
% wrong call
else
error("Argument für Format der exportierten Daten unzulässig!" + char(10) + "Bitte überprüfen Sie, wie die Daten aus LTspice exportiert wurden" + char(10) + "und nutzen eines der folgenden Argumente: [Time], [Bode], [Nyquist]." + char(10) + char(10) + "Der LTspice-Leitfaden im ISIS-Kurs gibt genauere Hinweise zur Verwendung von [readLTspice]." + char(10));
end
end
function T=readTFile(fileName)
textOriginal = '';
fid=fopen(fileName);
tline = fgetl(fid);
while ischar(tline)
textOriginal = textOriginal+"\n"+tline;
tline = fgetl(fid);
end
fclose(fid);
T = splitlines(compose(textOriginal));
end
function writeTFile(fileName,fileText)
fileID = fopen(fileName,'w');
fprintf(fileID,'%s\n',fileText);
fclose(fileID);
end
However, something always goes wrong:
What could I do? Thank you.

Risposte (1)

Voss
Voss il 11 Lug 2024 alle 18:34
T = readtable('Lab5_R_100_MOhm.txt','VariableNamingRule','preserve')
T = 401x7 table
Freq. Vn007/Vn001 Vn007/Vn001_1 Vn008/Vn003 Vn008/Vn003_1 Vn012/Vn005 Vn012/Vn005_1 ______ ___________ _____________ ___________ _____________ ___________ _____________ 10 -20.819 4.0965 -20.828 2.3008 -20.829 0.50166 10.233 -20.818 4.1536 -20.828 2.3162 -20.829 0.47517 10.471 -20.817 4.2128 -20.828 2.3328 -20.829 0.44893 10.715 -20.816 4.2742 -20.828 2.3507 -20.829 0.42293 10.965 -20.815 4.3379 -20.827 2.3698 -20.829 0.39716 11.22 -20.814 4.4039 -20.827 2.3901 -20.829 0.37159 11.482 -20.813 4.4721 -20.827 2.4118 -20.829 0.34623 11.749 -20.812 4.5428 -20.826 2.4347 -20.829 0.32104 12.023 -20.811 4.6157 -20.826 2.4589 -20.829 0.29603 12.303 -20.81 4.6911 -20.826 2.4843 -20.829 0.27117 12.589 -20.809 4.769 -20.825 2.5111 -20.829 0.24646 12.882 -20.807 4.8493 -20.825 2.5393 -20.829 0.22187 13.183 -20.806 4.9322 -20.825 2.5687 -20.829 0.19741 13.49 -20.805 5.0176 -20.824 2.5995 -20.829 0.17304 13.804 -20.803 5.1056 -20.824 2.6317 -20.829 0.14878 14.125 -20.802 5.1963 -20.824 2.6653 -20.829 0.12458
f = T.(1);
A = T.(2);
Ph = T.(3);
figure
tiledlayout(2,1)
nexttile()
semilogx(f,10*log10(-A))
ylabel('Magnitude')
nexttile()
semilogx(f,Ph)
xlabel('Frequency')
ylabel('Phase')

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by