how to pass values from a .txt file
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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')
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
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,:)
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
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')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!