How to plot the bode plot extracted from LTspice in Matlab?

26 visualizzazioni (ultimi 30 giorni)
Hello,
I'm trying to use matlab to plot the bode plot exported from LTspice. The .txt file as well as my code is added below. I found there maybe something wrong in the 'fopen' and 'textscan' part since there are no values for Dc and D, but I can't fix it. I really appreciate it if anyone can tell me how to solve it.
clc;
close all;
clear all:
fidi = fopen('CM filter.txt');
Dc = textscan(fidi, '%f (%f dB,%f °)','CollectOutput',1);
D = cell2mat(Dc);
figure
subplot(2,1,1)
semilogx(D(:,1), D(:,2))
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), D(:,3))
title('Phase (°)')
grid
xlabel('Frequency')

Risposta accettata

Les Beckham
Les Beckham il 4 Ago 2022
Modificato: Les Beckham il 4 Ago 2022
I used the import tool to import the data as fixed width data (right click on the file in the Current Folder and select Import Data.... Then use the ruler at the top to select the columns, separating the three columns of numbers from the other text. It resulted in the following:
%% Import data from text file.
% Script for importing data from the following text file:
%
% CM filter.txt
%
% To extend the code to different selected data or a different text file, generate a function instead of a script.
% Auto-generated by MATLAB on 2022/08/04 14:07:43
%% Initialize variables.
filename = 'CM filter.txt';
startRow = 2;
%% Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%22s%1s%24s%1s%22s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'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,3,5]
% 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
%% Split data into numeric and string columns.
rawNumericColumns = raw(:, [1,3,5]);
rawStringColumns = string(raw(:, [2,4,6]));
%% Make sure any text containing <undefined> is properly converted to an <undefined> categorical
for catIdx = [1,2,3]
idx = (rawStringColumns(:, catIdx) == "<undefined>");
rawStringColumns(idx, catIdx) = "";
end
%% Create output variable
CMfilter = table;
CMfilter.FreqVil_first_order = cell2mat(rawNumericColumns(:, 1));
CMfilter.VarName2 = categorical(rawStringColumns(:, 1));
CMfilter.filter = cell2mat(rawNumericColumns(:, 2));
CMfilter.VarName4 = categorical(rawStringColumns(:, 2));
CMfilter.VarName5 = cell2mat(rawNumericColumns(:, 3));
CMfilter.VarName6 = categorical(rawStringColumns(:, 3));
freq = CMfilter.FreqVil_first_order; % I added these three lines
mag = CMfilter.filter;
phase = CMfilter.VarName5;
%% Clear temporary variables
clearvars startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp rawNumericColumns rawStringColumns catIdx idx;
Now plot the bode plot.
tiledlayout(2,1)
nexttile
semilogx(freq,mag)
grid on
xlabel 'Freq (rad/sec)' % I'm guessing on the units here
ylabel 'Magnitude (dB)'
title(filename)
nexttile
semilogx(freq,phase)
grid on
xlabel 'Freq (rad/sec)' % I'm guessing on the units here
ylabel 'Phase (deg)'

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by