Files
imdd_silas/projects/Lab_cables_S21/HF Kabel/plot_cable.m
2026-03-25 10:57:48 +01:00

114 lines
3.7 KiB
Matlab

[file,path] = uigetfile("*.S2P");
filename = [path , file];
matrix = loads2p(filename);
frex = matrix(:,1);
for i = 1:numel(frex)
if frex(i) > 1e4
frex(i) = frex(i).*1e-9;
end
end
figure(101)
lines = numel(findall(gcf, 'type','line'))/4+1;
[CC, LL, MM] = ndgrid({1,2,3,4}, {'-', ':', '-.', '--'}, {'none','+', 'o', '*', 'square', 'diamond'});
combinations = [CC(:), LL(:), MM(:)];
cols = [0.3467 0.5360 0.6907
0.9153 0.2816 0.2878
0.4416 0.7490 0.4322
1.0000 0.5984 0.2000];
heads = ["S11","S12","S22","S21"];
for i = 1:4
subplot(2,2,i)
hold on
plot(frex,10*log10(abs(matrix(:,2*i)+1i.*matrix(:,2*i+1))),"Color",cols(combinations{lines,1},:),'DisplayName',file,'LineStyle',combinations{lines,2},'Marker',combinations{lines,3},'MarkerSize',3);
if i == 2 || i == 3
ylim([-6 0]);
else
ylim([-30 0]);
end
xlim([0 70]);
title(heads(i))
xlabel('Frequency in GHZ');
ylabel('Attenuation in dB');
legend
end
sgtitle(['S-Parameter']);
function matrix = loads2p(filepath)
startRow = 2;
%% Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%20s%25s%23s%23s%23s%23s%23s%23s%23s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filepath,'r');
%% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this code. If an error occurs for a different file, try regenerating the code from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric text to numbers.
% Replace non-numeric text with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1));
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3,4,5,6,7,8,9,10]
% Converts text in the input cell array to numbers. Replaced non-numeric text with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData(row), regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains(',')
thousandsRegExp = '^[-/+]*\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(char(strrep(numbers, ',', '')), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch
raw{row, col} = rawData{row};
end
end
end
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
matrix = cell2mat(raw);
%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp R;
end