Create Matlab figures in LaTeX using matlabfrag or alternatives

廉价感情. 提交于 2021-01-28 19:28:24

问题


I want to include Matlab figures into latex preferably with vectorised formats but with LaTeX MATLAB fonts for all text. I have been using the MATLAB function matlab2tikz which worked perfect for simple figures, but now my figures have too many data points which cause an error. So matlab2tikz is not suitable.

I've been looking at matlabfrag which I think will accomplish what I want, but when I run the script in LaTeX as detailed by the user guide it has an error File not found.

This is my code:

\documentclass{article}
\usepackage{pstool}
\begin{document}
\psfragfig{FileName}
\end{document}

Where FileName is the name of the .eps and .tex that matlabfrag creates. Has anyone come across this problem? Or recommend any other functions/methods to use?

I'm using Texmaker on Windows 7.


回答1:


My advice would be to rethink your workflow.

Instead of reusing your Matlab code to plot figures and be dissappointed by ever changing outputs with matlab2tikz, start reusing your latex code to plot figures and don't bother about plotting in Matlab anymore (at least not for beautiful plots).

matlab2tikz is just generating latex code based on the latex-package pgfplots. To understand the working of this package is pretty easy, as it is intended to be similar to Matlab.

So why bother and always let matlab2tikz do the work for you? Because again and again you won't be entirely happy with the results. Just try to write the pgfplots-code from scratch and just load the data from Matlab.

Here is a convenient function I wrote to create latex-ready text files:

function output = saveData( filename, header, varargin )

in = varargin;

numCols = numel(in);

if all(cellfun(@isvector, in))
    maxLength = max(cellfun(@numel, in));
    output = cell2mat(cellfun(@(x) [x(:); NaN(maxLength - numel(x) + 1,1)],in,'uni',0));
    fid = fopen(filename, 'w');
    fprintf(fid, [repmat('%s\t',1,numCols),'\r\n'], header{:});
    fclose(fid);
    dlmwrite(filename,output,'-append','delimiter','\t','precision','%.6f','newline', 'pc');
else
    disp('saveData: only vector inputs allowed')
end

end

Which could for example look like the following, in case of a bode diagram:

w   G0_mag  G0_phase    GF_mag  GF_phase    
10.000000   40.865743   -169.818991 0.077716    -0.092491
10.309866   40.345290   -169.511901 0.082456    -0.101188
10.629333   39.825421   -169.196073 0.087474    -0.110690
10.958700   39.306171   -168.871307 0.092787    -0.121071
11.298273   38.787575   -168.537404 0.098411    -0.132411

In your tikzpicture you can then just load that file by

\pgfplotstableread[skip first n=1]{mydata.txt}\mydata

and store the table into the variable \mydata.

Now check pfgplots how to plot your data. You will find the basic plot command \addplot

\addplot table [x expr= \thisrowno{0}, y expr= \thisrowno{3} ] from \mydata;

where you directly access the columns of your text file by \thisrowno{0} (confusing, I know).


Regarding your problem with to many data points: pgfplots offers the key each nth point={ ... } to speed things up. But I'd rather filter/decimate the data already in Matlab. The other way around is also possible, if you have to few data points the key smooth smoothes things up.



来源:https://stackoverflow.com/questions/40869706/create-matlab-figures-in-latex-using-matlabfrag-or-alternatives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!