read and use .txt file

8 views (last 30 days)
francesco baldi
francesco baldi on 26 Dec 2021
Commented: Walter Roberson on 27 Dec 2021
I was wondering if there's a way to read the .txt file i attached and use it to calculate other parameters. The .txt file reports the transfer function of my system with frequency, amplitude and phase respectively in the first, second and third column. Now, i need to calculate another parameter of the system in function of the transfer function. Assuming, for example, that my transfer function is called H, i need to calculate another parameter G = H*100/(1-H). Is there anyone who can tell me how to do it?
Star Strider
Star Strider on 26 Dec 2021
Reference — how to use LTSpice values in Matlab and earlier posts by the same OP, how to plot transfer function exported from LTSpice, how to use LTSpice values in Matlab, how to find the transfer function from LTspice, and perhaps others, since I may have missed a few of these prolific posts, all asking essentially the same question.

Sign in to comment.

Answers (2)

Meg Noah
Meg Noah on 26 Dec 2021
There's a lot of extra characters in your text file.
% frequency, amplitude and phase
str = fileread('draft.txt');
str = strrep(str,'{','');
str = strrep(str,'}','');
str = strrep(str,'(','');
str = strrep(str,')','');
str = strrep(str,'°','');
str = strrep(str,'''','');
str = regexprep(str, '\t', ' ');
str = regexprep(str, ' ', ' ');
str = regexprep(str, ' ', ',');
str = regexprep(str, 'dB', '');
objrec = regexp(str, '\r\n|\r|\n', 'split');
% remove empty cells (blank lines)
objrec(strlength(objrec)<1) = [];
nPts = numel(objrec);
frequency = zeros(nPts,1);
amplitude = zeros(nPts,1);
phase = zeros(nPts,1);
for iPt = 1:nPts
v = sscanf(objrec{iPt},'%f,%f,%f,');
frequency(iPt) = v(1);
amplitude(iPt) = v(2);
phase(iPt) = v(3);
omega = 2*pi*frequency;
H = 1./(1 + 1i*2*omega);
G = H*100.0./(1-H);
myTable = table(frequency,amplitude,phase,omega,H,G,'VariableNames', ...

Walter Roberson
Walter Roberson on 26 Dec 2021
It is not clear whether your second column is all negative, or if '(-' is the delimiter, the same way that the line ends in '-)' . The below code assumes that '(-' is the delimiter.
Your file does not have any degree symbols in it.
filename = '';
S = webread(filename);
data = cell2mat(textscan(S, '%f (-%fdB,%f%*[^\n]'));
whos data
Name Size Bytes Class Attributes data 301x3 7224 double
Walter Roberson
Walter Roberson on 27 Dec 2021
Let's experiment:
filename = '';
webopt = weboptions('CharacterEncoding', 'ISO-8859-1');
SW = webread(filename, webopt);
ans =
'4e+000°) 1'
ans = 1×11
52 101 43 48 48 48 176 41 13 10 49
That seems to have done the trick.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!