how to pass values from a .txt file
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
francesco baldi
le 3 Jan 2022
Commenté : Star Strider
le 4 Jan 2022
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 commentaires
Réponse acceptée
Star Strider
le 3 Jan 2022
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 commentaires
Walter Roberson
le 4 Jan 2022
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
le 4 Jan 2022
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.
Plus de réponses (1)
Walter Roberson
le 3 Jan 2022
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 commentaires
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!