read and use .txt file

hi,
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?

3 Comments

Rik
Rik on 26 Dec 2021
The first step is to read the file and extract the variables you need. What did you try for that step?
T1_a = readtable(filename, 'VariableNamingRule','preserve') %leggo il file come tabella
V2c_a = cellfun(@(x)sscanf(x, '(%fdB,%f°'), T1_a.Var2, 'Unif',0);
V2m_a = cell2mat(V2c_a')';
T2_a = table('Size',[size(T1_a.Var1,1) 3],'VariableTypes',{'double','double','double'}, 'VariableNames',{'FreqHz','MagndB','PhasDg'});
T2_a.FreqHz = T1_a.Var1;
T2_a.MagndB = V2m_a(:,1);
T2_a.PhasDg = V2m_a(:,2)
that's what i wrote, but i'm not sure if it is usefull honestly.
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)

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);
end
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', ...
{'frequency','amplitude','phase','omega','H','G'});
writetable(myTable,'myTable.xlsx');
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 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844820/draft.txt';
S = webread(filename);
data = cell2mat(textscan(S, '%f (-%fdB,%f%*[^\n]'));
whos data
Name Size Bytes Class Attributes data 301x3 7224 double

5 Comments

That's interesting. When I downloaded the OP 'draft.txt', the following is the first three lines:
1.00000000000000e+003 (-6.02488411418877e+000dB,-1.79940817416164e+000°)
1.02329299228075e+003 (-6.02508591811268e+000dB,-1.84129325097736e+000°)
1.04712854805090e+003 (-6.02529722271243e+000dB,-1.88415191829931e+000°)
...
To me, it looked like the amplitdue of line was was approximately -6.0249 dB. The degrees signs were evident following the values for phase.
1.00000000000000e+003 (-6.02488411418877e+000dB,-1.79940817416164e+000)
1.02329299228075e+003 (-6.02508591811268e+000dB,-1.84129325097736e+000)
1.04712854805090e+003 (-6.02529722271243e+000dB,-1.88415191829931e+000)
is what I see
The files seem to change. I had to deal with several different versions and formats in the posts I responded to. Those details are in the links I posted in my Comment to the original post. (I dealt with them by using readtable so I got the header information and essential format, then cellfun calling the sscanf function to parse them. It worked, although perhaps not as efficient as other approaches.)
Interesting. When I look at the file with Firefox, the line ends with -) . When I readtable() it has a degree sign. When I webread() it has U+65533
"U+FFFD (decimal 65533) is the "replacement character". When a decoder encounters an invalid sequence of bytes, it may (depending on its configuration) substitute for the corrupt sequence and continue. "
XCode and BBEdit both show the character as ∞ U+221E
When I look at the file as a hex dump, the character is U+00B0 which is the degree character.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844820/draft.txt';
SW = webread(filename);
SW(65:75)
ans =
'4e+000) 1'
ans+0
ans = 1×11
52 101 43 48 48 48 65533 41 13 10 49
ST = readtable(filename);
ST(1:3,:)
ans = 3×2 table
Var1 Var2 ______ ______________________________________________________ 1000 {'(-6.02488411418877e+000dB,-1.79940817416164e+000°)'} 1023.3 {'(-6.02508591811268e+000dB,-1.84129325097736e+000°)'} 1047.1 {'(-6.02529722271243e+000dB,-1.88415191829931e+000°)'}
Let's experiment:
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844820/draft.txt';
webopt = weboptions('CharacterEncoding', 'ISO-8859-1');
SW = webread(filename, webopt);
SW(65:75)
ans =
'4e+000°) 1'
ans+0
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.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 26 Dec 2021

Commented:

on 27 Dec 2021

Community Treasure Hunt

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

Start Hunting!