Tex series: drawing with MATLAB and latex

Source: Internet
Author: User

The purpose is to plot in MATLAB and use pgfplots in latex to modify the graph, such as coordinate system, coordinate axis mark, and title. This ensures the consistency between the markup in the graph and the text in the text, and makes use of MATLAB's powerful data processing and rich plotting functions. The entire process is smooth. You can try it.

 

Assume that the latex file is in the E: \ Tex folder. The script file consisting of the following Matlab code is saved in this folder. Change the current directory of MATLAB to E: \ Tex, run the MATLAB script to obtain the following pdf file in the current directory.

 

Matlab code:

% First figure figname = 'fig1'; % plot x = linspace (100 * Pi, 20); plot (x, sin (x) +, '-O ', x, 100 + X. ^ 2/20) % plot end axis offset (GCA, 'position', [,]) Set (GCF, 'papersize', []) 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, 'xl Im '); get (GCA, 'ylim'); get (GCA, 'zlim')], ''); % second figure figname = 'fig2 '; % plot peaks (300); shading interpview ([]) % plot end axis offset (GCA, 'position', [,]) 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')], ''); % compile latex document! Pdflatex exam. Tex

Latex document (exam. Tex ):

\ Documentclass [a4paper] {Article} \ usepackage {CJK} \ usepackage {pgfplots} \ usepackage {pgfplotstable} \ pgfplotsset {width = 8 cm, 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} This is the first image \ 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} \ {\ loadedtable} \ edef \ ymin {\ pgfplotsretval} \ pgfplotstablegetelem {1} {[Index] 1 }\of {\ loadedtable} \ edef \ Ymax {\ pgfplotsretval} \ begin {center} This is the second figure \ 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}

 

There may be a better way to use MATLAB 2tikz to directly convert MATLAB graphics into tikzp/gfplots code. See

Http://blog.csdn.net/mathsoperator/article/details/6826208

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.