TeX系列: MATLAB和LaTeX结合绘图
目的是在MATLAB中绘图,在LaTeX中利用PGFPlots实现对图的修饰,比如坐标系、坐标轴标记、标题等等。这样能够保证图的中标记与正文文本的一致性,同时又可利用MATLAB强大的数据处理和丰富的绘图功能。整个过程比较顺畅,可以一试。
假设LaTeX文档所在的文件夹是E:\TeX,如下MATLAB代码构成的脚本文件保存在该文件夹下,改变MATLAB的当前目录为E:\TeX,执行该MATLAB脚本,在当前目录下得到如下pdf文档。
MATLAB代码:
- %% 第一个图
- figname='fig1';
- % 绘图
- x=linspace(0,3*pi,20);
- plot(x,sin(x)+100,'-o',x,100+x.^2/20)
- % 绘图结束
- axis off
- set(gca,'Position',[0,0,1,1])
- set(gcf,'PaperSize',[8,8])
- set(gcf,'PaperPosition',[0.01,0.01,7.98,7.98])
- lim=get(gca,'xlim');
- set(gca,'xlim',[lim(1) lim(2)+(lim(2)-lim(1))/19])
- lim=get(gca,'ylim');
- set(gca,'ylim',[lim(1) lim(2)+(lim(2)-lim(1))/19])
- print('-dpdf',figname);
- dlmwrite([figname '.dat'],[get(gca,'xlim');get(gca,'ylim');get(gca,'zlim')],' ');
- %% 第二个图
- figname='fig2';
- % 绘图
- peaks(300);
- shading interp
- view([0,90])
- % 绘图结束
- axis off
- set(gca,'Position',[0,0,1,1])
- set(gcf,'PaperSize',[8,8])
- set(gcf,'PaperPosition',[0.01,0.01,7.98,7.98])
- set(gca,'XLimMode','manual')
- print('-dpdf',figname);
- dlmwrite([figname '.dat'],[get(gca,'xlim');get(gca,'ylim');get(gca,'zlim')],' ');
- %% 编译LaTeX文档
- !pdflatex exam.tex
LaTeX文档(exam.tex):
- \documentclass[a4paper]{article}
- \usepackage{CJK}
- \usepackage{pgfplots}
- \usepackage{pgfplotstable}
- \pgfplotsset{width=8cm,compat=1.3}
- \begin{document}
- \begin{CJK*}{GBK}{kai}
- \thispagestyle{empty}
- \pgfplotstableread{fig1.dat}\loadedtable
- \pgfplotstablegetelem{0}{[index]0}\of{\loadedtable}
- \edef\xmin{\pgfplotsretval}
- \pgfplotstablegetelem{0}{[index]1}\of{\loadedtable}
- \edef\xmax{\pgfplotsretval}
- \pgfplotstablegetelem{1}{[index]0}\of{\loadedtable}
- \edef\ymin{\pgfplotsretval}
- \pgfplotstablegetelem{1}{[index]1}\of{\loadedtable}
- \edef\ymax{\pgfplotsretval}
- \begin{center}
- 这是第一个图
- \end{center}
- \begin{tikzpicture}
- \begin{axis}[axis x line=bottom, axis y line=left,enlargelimits=false,axis on top]
- \addplot graphics [xmin=\xmin,xmax=\xmax, ymin=\ymin,ymax=\ymax] {fig1.pdf};
- \end{axis}
- \end{tikzpicture}
- \pgfplotstableread{fig2.dat}\loadedtable
- \pgfplotstablegetelem{0}{[index]0}\of{\loadedtable}
- \edef\xmin{\pgfplotsretval}
- \pgfplotstablegetelem{0}{[index]1}\of{\loadedtable}
- \edef\xmax{\pgfplotsretval}
- \pgfplotstablegetelem{1}{[index]0}\of{\loadedtable}
- \edef\ymin{\pgfplotsretval}
- \pgfplotstablegetelem{1}{[index]1}\of{\loadedtable}
- \edef\ymax{\pgfplotsretval}
- \begin{center}
- 这是第二个图
- \end{center}
- \begin{tikzpicture}
- \begin{axis}[enlargelimits=false,axis on top]
- \addplot graphics [xmin=\xmin,xmax=\xmax, ymin=\ymin,ymax=\ymax] {fig2.pdf};
- \end{axis}
- \end{tikzpicture}
- \end{CJK*}
- \end{document}
也许有一个更好的办法,使用matlab2tikz把matlab图形直接转化成tikzp/gfplots代码. 参见
http://blog.csdn.net/mathsoperator/article/details/6826208
from: http://blog.csdn.net/mathsoperator/article/details/6821336
TeX系列: MATLAB和LaTeX结合绘图的更多相关文章
- TeX 家族(TeX, pdfTeX, XeTeX, LuaTeX, LaTeX, pdfLaTeX, XeLaTeX …)
TeX 家族 带有 TeX 的词,仅仅是本文就已经提到了 TeX, LaTeX, XeLaTeX.通常中国学生面对不了解意思的一群形近单词,都会有一种「本能的恐惧」(笑~).因此,「大神们」在为新手介 ...
- matlab构建栅格地图绘图思路
matlab构建栅格地图绘图思路 近来因研究需要,调研并思考了栅格地图的生成方法,暂时总结以备不时之需. 栅格的建立最需要注意栅格粒度的问题,即根据需要调整栅格的边长,目前有两种思路一种是固定栅格边长 ...
- TeX系列: tikz-3dplot绘图宏包
tikz-3dplot包提供了针对TikZ的命令和坐标变换样式, 能够相对直接地绘制三维坐标系统和简单三维图形. tikz-3dplot包当前处于初创期, 有很多功能有待完善. 安装过程如下: (1) ...
- matlab作图 latex插图
推荐用saveas eps,再用eps2pdf转成pdf.这样可以之间pdflatex编译. if result.savepic saveas(gcf,[ pwd '/picture/right_' ...
- Tex系列: pgfplots安装
(1) 上网下载最新宏包压缩包 http://sourceforge.net/projects/pgfplots/files/pgfplots/ (2)解压压缩包,把该包下的tex子目录拷贝至D:\ ...
- matlab figure 论文级别绘图
1.将figure调整为最大: figure;set(gcf,'outerposition',get(0,'screensize')); 2.获得figure中的大小 [x,y] = ginput 3 ...
- matlab实现贝塞尔曲线绘图pdf查看
贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...
- matlab图片 latex显示
matlab图片另存为bmp格式,用在线网站转换格式为png. 莫要直接另存为jpg格式,在latex中显示,否则图片显示的质量不高.
- Html5 学习系列(五)Canvas绘图API快速入门(2)
Canvas绘图API Demos 上一篇文章中,笔者已经给大家演示了怎么快速用Canvas的API绘制一个矩形出来.接下里我会在本文中给各位介绍Canvas的其他API:绘制线条.绘制椭圆.绘制图片 ...
随机推荐
- (转载)Linux入门:操作目录和文件的命令
PATH 每个用户的PATH都是不一样的: PATH中不包含“当前目录”: (1)echo $PATH:显示PATH环境变量: (2)PATH = "$PATH":/home/ ...
- oracle 12C安装问题
1. 先弄好c$ share的问题 2. 测试一下 c$ share 是否成功. 方法是在cmd里打net use \\localhost\c$ 失败会是这样子...: 系统错误53 The ne ...
- AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345
[USACO5.4]奶牛的电信Telecowmunication 思路: 水题: 代码: #include <cstdio> #include <cstring> #inclu ...
- 第七章 用户输入和while语句
大多数编程都旨在解决最终用户的问题,为此通常需要从用户那里获取一些信息.例如,假设有人要判断自己是否到了投票的年龄,要编写回答这个问题的程序,就需要知道用户的年龄,这样才能给出答案.因此,这种程序需要 ...
- 在ubuntu下安装kaldi基本步骤
注:最近在学习kaldi语音识别工具,在安装过程中遇到了许多问题,在此记录,以备后需. 在一开始,我看了这篇博客(http://blog.topspeedsnail.com/archives/1001 ...
- linq to sql: 在Entityfamework Core中使用多个DbContext
最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext, 发blog的时候需要用到Account的信息,但是再Blog中只记 ...
- rsync: chroot No such file or directory (2)
rsync: ) 查了N多资料,均未解决,最终发现是因为report后面多了个空格...
- Redis实战(一)
一.准备 Redis 是一个开源的使用ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库.Redis的出现,很大程度补偿了memcached这类key/valu ...
- AngularJS Intellisense in Visual Studio 2012
Recently, a lot of people have asked for Intellisense support for AngularJS in the Visual Studio HTM ...
- Java常用工具类之时间转换(注释乱码,全)
package com.wazn.learn.util; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...