我的建议是重新考虑您的工作流程。
不要重复使用您的 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 可以使事情顺利进行。