【问题标题】:Create Matlab figures in LaTeX using matlabfrag or alternatives使用 matlabfrag 或替代方法在 LaTeX 中创建 Matlab 图形
【发布时间】:2017-04-13 16:11:48
【问题描述】:

我想将 Matlab 图形包含到乳胶中,最好使用矢量化格式,但所有文本都使用 LaTeX MATLAB 字体。我一直在使用 MATLAB 函数 matlab2tikz,它非常适合简单的数字,但现在我的数字有太多的数据点会导致错误。所以matlab2tikz不适合。

我一直在查看matlabfrag,我认为它可以实现我想要的,但是当我按照用户指南的详细说明在 LaTeX 中运行脚本时,它会出现错误File not found

这是我的代码:

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

其中FileNamematlabfrag 创建的.eps 和.tex 的名称。有没有人遇到过这个问题?或者推荐其他使用的功能/方法?

我在 Windows 7 上使用 Texmaker。

【问题讨论】:

    标签: matlab plot latex matlab-figure eps


    【解决方案1】:

    我的建议是重新考虑您的工作流程。

    不要重复使用您的 Matlab 代码来绘制图形 并因matlab2tikz 不断更改输出而感到失望,开始重复使用您的 latex 代码以绘制图形,并且不再为在 Matlab 中绘制而烦恼(至少不是为了漂亮的情节)。

    matlab2tikz 只是基于乳胶包pgfplots 生成乳胶代码。理解这个包的工作很容易,因为它类似于 Matlab。

    那么,为什么要麻烦并总是让matlab2tikz 为您完成工作呢?因为一次又一次你不会对结果完全满意。只需尝试从头开始编写 pgfplots 代码,然后只需从 Matlab 加载数据

    这是我编写的一个方便的函数,用于创建可用于乳胶的文本文件:

    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
    

    如果是波特图,它可能如下所示:

    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
    

    在您的tikzpicture 中,您可以通过以下方式加载该文件

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

    并将表格存储到变量\mydata中。

    现在检查pfgplots 如何绘制数据。你会发现基本的绘图命令\addplot

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

    您可以通过\thisrowno{0} 直接访问文本文件的列(我知道这很令人困惑)。


    关于许多数据点的问题:pgfplots 提供了密钥each nth point={ ... } 来加快速度。但我宁愿过滤/抽取已经在 Matlab 中的数据。 反之亦然,如果您需要很少的数据点,则键 smooth 可以使事情顺利进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2022-08-24
      • 2014-12-19
      相关资源
      最近更新 更多