how to pass values from a .txt file

4 ビュー (過去 30 日間)
francesco baldi
francesco baldi 2022 年 1 月 3 日
コメント済み: Star Strider 2022 年 1 月 4 日
Hi there,
I have the following problem: I exported the transfer function of a circuit as a .txt file from LTSpice to Matlab. The txt file contains a list of frequency, real part values and imaginary part values of my transfer function. Now, i need to use these values to calculate another parameter of my circuit, always in its real and imaginary parts, but i don't know how to do it.
Supposing that the transfer function is called H, i need to calculate real and immaginary part of the parameter G for all the frequencies in the .txt:
Re(G) = (100*(Re(H) - (Re(H))^2 - (Im(H))^2))/(1 + (Re(H))^2 - 2*Re(H) + (Im(H))^2)
Im(G) = (100*Im(H))/(1 + (Re(H))^2 - 2*Re(H) + (Im(H))^2)
i attached the .txt file of the transfer function H.

採用された回答

Star Strider
Star Strider 2022 年 1 月 3 日
Reading the file was another interesting challenge!
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/851740/RC_parallelo_A.txt', 'VariableNamingRule','preserve')
T1 = 301×2 table
Freq. V(n002)/V(n001) ______ ________________________________________________ 1000 {'4.99507006345180e-001,-1.56924754155065e-002'} 1023.3 {'4.99483796257063e-001,-1.60572539733348e-002'} 1047.1 {'4.99459494622917e-001,-1.64304760271490e-002'} 1071.5 {'4.99434050222423e-001,-1.68123344493718e-002'} 1096.5 {'4.99407409442751e-001,-1.72030263342230e-002'} 1122 {'4.99379516167860e-001,-1.76027530768304e-002'} 1148.2 {'4.99350311662788e-001,-1.80117204528228e-002'} 1174.9 {'4.99319734452691e-001,-1.84301386983247e-002'} 1202.3 {'4.99287720196435e-001,-1.88582225902587e-002'} 1230.3 {'4.99254201554475e-001,-1.92961915268536e-002'} 1258.9 {'4.99219108050801e-001,-1.97442696082487e-002'} 1288.2 {'4.99182365928685e-001,-2.02026857170701e-002'} 1318.3 {'4.99143897999976e-001,-2.06716735988481e-002'} 1349 {'4.99103623487659e-001,-2.11514719421283e-002'} 1380.4 {'4.99061457861407e-001,-2.16423244581193e-002'} 1412.5 {'4.99017312665835e-001,-2.21444799597034e-002'}
T12 = cell2mat(T1{:,2});
for k = 1:size(T12,1)
T1{:,2}{k,:} = str2num(strrep(T12(k,:), ',',' '));
end
Freq = T1.('Freq.');
V = cell2mat(T1{:,2});
ReV = V(:,1);
ImV = V(:,2);
H = ReV + 1j*ImV;
Re = @(H) real(H);
Im = @(H) imag(H);
ReG = (100*(Re(H) - (Re(H)).^2 - (Im(H)).^2))./(1 + (Re(H)).^2 - 2*Re(H) + (Im(H)).^2);
ImG = (100*Im(H))./(1 + (Re(H)).^2 - 2*Re(H) + (Im(H)).^2);
CxG = ReG + 1j*ImG;
figure
subplot(2,1,1)
semilogx(Freq, mag2db(abs(CxG)))
grid
ylabel('Magnitude (dB)')
subplot(2,1,2)
semilogx(Freq, rad2deg(angle(CxG)))
grid
xlabel('Frequency')
ylabel('Phase (°)')
sgtitle('Transfer Function Bode Plot of ‘G’')
.
  13 件のコメント
Walter Roberson
Walter Roberson 2022 年 1 月 4 日
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/852285/RLCAcart.txt';
S = webread(filename);
data = cell2mat(textscan(S, '%f %f,%f', 'headerlines', 1));
format long g
data(1:5,:)
ans = 5×3
1.0e+00 * 1000 8.90096842969402e-05 0.00628442202450237 1023.29299228075 9.27357685256782e-05 0.00643087677904904 1047.1285480509 9.66376954297881e-05 0.00658074803514854 1071.51930523761 0.000100723775170423 0.00673411574036376 1096.47819614319 0.000105002711771054 0.00689106173072146
Star Strider
Star Strider 2022 年 1 月 4 日
I didn’t even consider webread since I very seldom do anything with MATLAB and web pages, so I have little experience with it. That’s certainly a work-around to avoid the problems with fopen.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 1 月 3 日
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/851740/RC_parallelo_A.txt';
S = webread(filename);
parts = regexp(S, '(?<freq>-?\d\S+)\s+(?<real>-?\d[^\s,]+),(?<imag>-?\d\S)', 'names');
freqs = str2double({parts.freq});
ReH = str2double({parts.real});
ImH = str2double({parts.imag});
ReG = (100*(ReH - (ReH).^2 - (ImH).^2))./(1 + (ReH).^2 - 2*ReH + (ImH).^2);
ImG = (100*ImH)./(1 + (ReH).^2 - 2*ReH + (ImH).^2);
G = complex(ReG, ImG);
[sReH, hIdx] = sort(ReH); sImH = ImH(hIdx);
plot(sReH, sImH); title('real H vs imag H')
[sReG, gIdx] = sort(ReG); sImG = ImG(gIdx);
plot(sReG, sImG); title('real G vs imag G')

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by