使用matLabfrag或替代方案在LaTeX中创建MatLab图形 [英] Create Matlab figures in LaTeX using matlabfrag or alternatives
问题描述
我想包括LaTeX的数字,最好是与矢量化的格式,但与LaTeX的所有文本的MatLax字体。我一直在使用MatLab函数matlab2tikz
,它对简单的图形非常有效,但现在我的图形有太多的数据点,这会导致错误。因此matlab2tikz
不适合。
我一直在查看matlabfrag
,我认为它可以实现我想要的功能,但当我按照用户指南中的详细说明在LaTeX中运行该脚本时,它出现错误File not found
。
这是我的代码:
documentclass{article}
usepackage{pstool}
egin{document}
psfragfig{FileName}
end{document}
其中FileName
是matlabfrag
创建的.ep和.tex的名称。有没有人遇到过这个问题?或推荐使用任何其他函数/方法?
我在Windows 7上使用的是Texaker。
推荐答案
我的建议是重新考虑您的工作流程。
不再重复使用MatLab代码来绘制图形并对matlab2tikz
不断变化的输出感到失望,而是开始重新使用LaTeX代码来绘制图形,不再费心在MatLab中绘制图形(至少不是用于绘制漂亮的图形)。
matlab2tikz
只是基于LaTeX包pgfplots
生成LaTeX代码。理解此包的工作原理非常容易,因为它旨在类似于MatLab。
matlab2tikz
为您做这项工作呢?因为一次又一次,你不会对结果完全满意。只需尝试从头开始编写pgfplots-code,只需从Matlab加载数据。
我编写了一个方便的函数来创建支持LaTeX的文本文件:
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 ',1,numCols),'
'], header{:});
fclose(fid);
dlmwrite(filename,output,'-append','delimiter',' ','precision','%.6f','newline', 'pc');
else
disp('saveData: only vector inputs allowed')
end
end
例如,在bode图的情况下,它可能如下所示:
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
中。
addplot
addplot table [x expr= hisrowno{0}, y expr= hisrowno{3} ] from mydata;
您通过 hisrowno{0}
直接访问文本文件的列(我知道这很令人困惑)。
关于多个数据点的问题:pgfplots提供了加快速度的关键。但我更愿意过滤/抽取已在MatLab中的数据。
另一种方法也是可能的,如果您必须使用几个数据点,smooth
键可以使事情变得更顺利。
这篇关于使用matLabfrag或替代方案在LaTeX中创建MatLab图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!