The studies at a university sometimes serve the students some unwanted surprises. I have to do some homework for a lecture in LTSpice. LTSpice is a wonderful tool for modeling and simulation of electrical circuits, but the plot-export for example, papers is not really satisfying.
To create plots for papers and homework my tool of choice is Matlab, it has the advantage that the programmer can completely configure all plot attributes and use custom values.
Due to the fact that LTSpice has the functionality to export data in a *.txt – file I decided to create a Matlab function for my plot’s and below you can find how I did it.
// LTSpice data export
The data export in LTSpice is very simple. When the plot window is checked, choose file and then export. The appearing window shows the possible plot data’s that can be exported. Just select the data that should be exported and LTSpice does the rest.
The exported file looks like this.
time V(vbiastest)
0.000000000000000e+000 8.199954e-001
2.256562492220837e-006 8.341131e-001
4.209687492220837e-006 8.461137e-001
6.162812492220837e-006 8.577214e-001
8.115937492220837e-006 8.687617e-001
1.006906249222084e-005 8.790685e-001 |
// Reading the data with Matlab
First of all it is important to read in the data so you can edit it in Matlab. To read in from a file it is necessary to open that file, like in any other programming language too. In Matlab there is the function “fopen(‘Filename’)” which does the work for you. This function returns the file – ID which is necessary for further processing. To read in the file I used the function “textscan(fileID, ‘%s’)”. This function reads the whole file into a string and returns the content of the file in a cell – object. That was the whole magic of reading in a file to Matlab and at the end it is always important to close the file when the read/write process is done so the function “fclose(fileID)” should always be at the end of the read in process.
Here below is the whole commented source code of file reading
fileID = fopen('input_files/opv_input_stage_n_m_test_transient.txt');
fContent = textscan(fileID, '%s'); % reads the whole file into fContent
fclose(fileID);
fContent = fContent{1}; % making fContent to an n x 1 - Matrix |
// Expanding and re-modeling the data
The main and more interesting part is to extract the data from the string array so Matlab can plot it.
As we’ve seen above the file starts with the name of the axis. In the code below you can find the code in which the axis names were stored in the variables, very trivial as you can see.
%% create axis and plot names
title = fContent(2);
if strcmp(fContent(1), 'Freq.')
xAxis = 'Frequ';
end
if strcmp(fContent(1), 'time')
xAxis = 'Time';
end |
LTSpice offers the engineer the possibility to simulate an electronic circuit with a parameter sweep. This sweep can be made with the “.step param” – command in LTSpice. In this case the LTSpice-file holds more than one plot. For this case we add a variable to the program for checking if there are more plots to show.
In the source code below the check of the parameter sweep simulation is shown and also the calculation of the step size on the x-axis. For the calculation of the time step the “str2double” – function can be used; it does all the type conversion for you.
%% create axis and plot names
%% determine if parameter step simulation
if strcmp(fContent(3), 'Step')
stepSim = 1;
end
%% determine x - axis step size
str1 = fContent(3);
str2 = fContent(5);
if stepSim
str1 = fContent(8);
str2 = fContent(10);
end
xStepsize = str2double(str2) - str2double(str1); |
At this point the Matlab – code has extracted all parameters it needs to store all variables into an array or matrix for the plot.
In the code below the first thing I’ve done is to set the counter variable to the first value that holds a value for the plot. Mind that there is an if-query to check if there are more plots than one to show. In this case the first value is on a different place.
After that Matlab can start to convert all the plot variables from the input matrix into a new plot matrix. In case that there is only one plot to show the while-loop ends with the a-array full of double-values that is at the end of the loop stored to A.
In case that there are more plot’s to show the while loop has to do a lot more work than for one plot. If there is no fixed step size set in LTSpice, it’s a common case that LTSpice changes the step size between the simulations and the simulation-output has different length’s. Arrays with different lengths cannot be stored in the same matrix and it’s also not possible to display it in the same plot. Matlab serves the functionality of linear interpolation in the function “interp1″.
The “interp1″-function does the linear interpolation for us and gives us the array with the length we need for our matrix and we can expand the matrix with the vector.
For those of you that are more interested in this topic i would please you to read the Matlab website at this topic, the link can be found at the end of this blog.
In the source code below all the functionality described above is posted.
i = 4;
if stepSim
i = 9;
end
while( i |
// The plot
The plot is another mighty tool of Matlab. It serves a lot of properties that can be modified or changed. In the source code my most used properties were shown.
I think the source code speaks for it selves and no much explanation needs to be taken. In case you need some, let me know I will help!
%plot
figure();
for i=1:length(A(:,1));
plot(A(i,:));
hold on;
fig = gcf;
end
hold off;
grid on;
maxVal = A(1,1);
for i=1:length(A(:,1))
if maxVal < max(A(i,:))
maxVal = max(A(i,:));
end
end
minVal = A(1,1);
for i=1:length(A(:,1))
if minVal > min(A(i,:))
minVal = min(A(i,:));
end
end
% Axes propertiesset
axis([0 length(A(1,:)) minVal maxVal*1.1])
set(gca, 'Box', 'off', ...
'FontSize', 25, ...
'LineWidth', 2, ...
'TickDir', 'out', ...
'XTickLabel',0:length(A(i,:))/10:length(A(i,:)), ...
'XTick', 0:length(A(i,:))/10:length(A(i,:)), ...
'XGrid', 'on', ...
'XTickMode','man',...
'YTickLabel',0:(maxVal/10):maxVal*1.1, ...
'YTick', 0:(maxVal/10):maxVal*1.1);
xlabel(xAxis, 'FontSize', 60)
ylabel(yAxis, 'FontSize', 60)
% Line properties
line_handles = findobj('Type','Line');
set(line_handles(1), 'LineStyle', '-', ...
'LineWidth', 4, ...
'Color', 'Black'); |
// Conclusio
This code above shows up how easy and fast a plot routine in Matlab can be written. If you like the code and it helped you feel free to copy it, rewrite it or change it.
// Links
Matlab Interpolation
251f