For your specific data file in order to not have to modify the file, the following should return the data in usable fashion...
fid=fopen('touchstone_file.txt','r');
while ~feof(fid)
l=fgetl(fid);
if startsWith(l,'#'),break,end
end
R=str2num(strip(extractAfter(l,['R' char(9)])));
while ~feof(fid)
l=fgets(fid);
if ~startsWith(l,{'#','!'}),break,end
end
fseek(fid,-length(l),0)
S=cell2mat(textscan(fid,'','CollectOutput',1));
fid=fclose(fid);
clear i;
tS=table(S(:,1), S(:,2)+i*S(:,3), S(:,4)+i*S(:,5), S(:,6)+i*S(:,7), S(:,8)+i*S(:,9), ...
'VariableNames',{'Freq','S12','S11','S21','S22'});
S=sparameters(tS{:,2:end},tS.Freq,R(1));
The MATLAB function can only take one R value as discussed elsewhere, though.
For your file and any other of same shape as this one, this returns
>> R
R =
33.7558 50.0000
>> tS(1:10,:)
ans =
10×5 table
Freq S12 S11 S21 S22
_______ __________________ ___________________ _____________________ _________________
5e+09 0.99984+0.015619i -0.10198+0.0047799i 3.0572e-05+0.0019382i 0.82423-0.038319i
5.2e+09 0.99985+0.014558i -0.10195+0.0050565i 3.4759e-05+0.0020153i 0.82406-0.039836i
5.4e+09 0.99986+0.01356i -0.10193+0.0053301i 3.9108e-05+0.0020924i 0.82388-0.04135i
5.6e+09 0.99987+0.012617i -0.10191+0.005601i 4.3621e-05+0.0021696i 0.82369-0.042863i
5.8e+09 0.99988+0.011723i -0.10189+0.0058696i 4.8299e-05+0.0022468i 0.8235-0.044374i
6e+09 0.99988+0.010873i -0.10187+0.0061361i 5.314e-05+0.002324i 0.8233-0.045882i
6.2e+09 0.99989+0.010063i -0.10186+0.0064007i 5.8145e-05+0.0024013i 0.8231-0.047389i
6.4e+09 0.99989+0.0092902i -0.10184+0.0066635i 6.3315e-05+0.0024787i 0.82289-0.048893i
6.6e+09 0.99989+0.0085497i -0.10183+0.0069249i 6.8649e-05+0.002556i 0.82267-0.050396i
6.8e+09 0.99989+0.0078391i -0.10182+0.0071848i 7.4147e-05+0.0026334i 0.82245-0.051896i
>>
I presume if there's more to the struct sparamteters creates in using builtin methods there's probably a way to stuff these data into it...altho you may run into a limitation that it doesn't know how to handle the R array, either.
ADDENDUM:
The information for the sparameters function includes the optional form:
"sobj = sparameters(data,freq, Z0) creates an S-parameter object from the S-parameter data, data, and frequencies, freq, with a given reference impedance Z0." Unfortunately, it also states that the Z0 reference impedance is only a single reference value, not one per channel.
So,
S=sparameters(tS{:,2:end},tS.Freq,R(1));
should give you a compatible object but with the limitation of only one reference R. But, that's going to be a limitation on everything the whole suite does with the object.
I suppose one could incorporate the two values in the calculation of the returned results into the scaling of the various product terms and then use a reference of 1 for the object parameter.
Or, alternatively scale the return channel data from other than 50 (say) to 50 as if both channels used 50 (or vice versa to use other channel reference). Same idea.
As noted in the Comment below, this seems a real shortcoming in the implementation worthy of support request.
3 Comments
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_832896
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_832896
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_833213
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_833213
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_833216
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/519848-touchstone-file-could-not-be-processed-in-matlab#comment_833216
Sign in to comment.