TeX - LaTeX Asked on May 22, 2021
I’m trying to plot the modified Bessel functions using latex, so i tried the following
I generated the following .txt file in Matlab
z = linspace(0,20);
scale = 1;
Ks = zeros(4,100);
for nu = 0:3
Ks(nu+1,:) = besselk(nu,z,scale);
end
save('data','Ks','z')
and then I tried the following in latex
documentclass{standalone}
usepackage{pgfplots}
usepackage{pgfplotstable}
pgfplotsset{compat=1.10}
pgfplotstableread{
include{Data}
}datatable
begin{document}
begin{tikzpicture}
begin{axis}
addplot table {datatable}
;
end{axis}
end{tikzpicture}
end{document}
but I’m not getting an output.
You're doing a couple of things wrong. First of all, the save
function in Matlab saves a binary .mat
file unless you specify otherwise, and pgpflotstableread
can't read .mat
files. You need to add the -ascii
option, or use dlmwrite
, fprintf
or some other function that will write data to text files.
Next, you're not supposed to use include
inside pgfplotstableread
, but add the filename directly, i.e. pgfplotstableread{filename.txt}
(The extension doesn't have to be .txt
.) By default pgfplotstableread
assumes that columns in the text file are delimited by spaces, if you're using a different column separator you have to specify that, e.g. pgfplotstableread[col sep=comma]{...}
if commas are used.
Finally, you probably have to choose which columns to plot. When you do addplot table {datatable};
, pgfplots
will use the first column for the x-values and the second column for the y-values. To plot multiple columns, you need multiple addplot
s, e.g.
addplot table[x index=0, y index=1] {datatable};
addplot table[x index=0, y index=2] {datatable};
If the columns are named, you can use e.g. x=NameOfXColumn
instead of x index=0
.
Working example follows. I used Octave via OctaveOnline, but I think the code would be the same in Matlab.
z = linspace(0,20);
scale = 1;
Ks = zeros(4,100);
for nu = 0:3
Ks(nu+1,:) = besselk(nu,z,scale);
end
% Put both variables in one matrix
% transpose them to make the data column oriented - the dimensions of dataOut is 100 x 5
dataOut = [z' Ks'];
% save the new matrix using the `-ascii` option
save('data.dat','dataOut', '-ascii')
The LaTeX code:
documentclass{standalone}
usepackage{pgfplotstable}
pgfplotsset{compat=1.10}
% add file name directly, no input or include
pgfplotstableread{data.dat}datatable
begin{document}
begin{tikzpicture}
begin{axis}[ymode=log]
addplot table {datatable};
addplot table[y index=1] {datatable};
addplot table[y index=2] {datatable};
addplot table[y index=3] {datatable};
addplot table[y index=4] {datatable};
legend{$nu=0$, $nu=1$, $nu=2$, $nu=3$}
end{axis}
end{tikzpicture}
end{document}
Correct answer by Torbjørn T. on May 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP