PGFPlots – How to Solve matlab2tikz ‘TeX Out of Memory’ Error

MATLABmemorypgfplots

I am using matlab2tikz to import figures from MATLAB to tex.

However I quickly get the message TeX capacity exceeded, sorry [main memory size = 3000000].

I have tried expand main memory using instructions from http://docs.miktex.org/manual/runtimeparams.html
using
initexmf --edit-config-file=xelatex
but that only opens an empty file!

I have also tried to use externalization functionality of pgfplots using instructions from its manual and also from Export eps figures from TikZ, but I've also failed (probably because I am using xetex and not pdflatex).

What should I do furhter?

EDIT: I am using MikTeX 2.9 on Windows 7 (with XeTeX 0.9998)

Best Answer

Since I didn't have any luck with altering the TeX memory capacity, I had to find another way to solve my problem.

It was matlab2tikz that actually did the job. It has an option of downsampling the figures during the conversion to tikz-pgf. It is called minimumPointsDistance.

The final (downsampled) figures where absolutely indistinguishable from the original and MUCH, MUCH more light. Also note, that you don't have to downsample the figures in MATLAB yourself, but just set the minimumPointsDistance which only affects matlab2tikz's output code.

EDIT: This is an expansion of my answer. I apologise for getting out of the TeX limits, but this extension offers a better solution, so I believe it should be posted here. Now on the problem...

matlab2tikz's point reduction algorithm although fast and simple has a

main disadvantage

It can distort certain "stiff" areas of the curve when the actual number of points kept drops very low. This is illustrated in the following picture

enter image description here

The blue curve (1184 points) is the original while the red one (117 points) is the reduced. The distortion is obvious.

To deal with this and at the same time keep the number of points low I wrote a new point reduction algorithm which is much more effective (but can be slower sometimes). The results (red curve is 116 points)

enter image description here

It is clear that the distortion is gone.

But there still is

one more disadvantage

that exists in both algorithms. When one converts a batch of figures to tikz, they usually want uniform quality for all reduced diagrams. But sometimes this is not possible (at least without extra effort) because typically minimumPointsDistance must be set differently for each one of the figures and the results actually depend on the size of the intervals [min_y, max_y] and [min_x, max_x] as well as the number of points in the figure.

So in order to overcome this problem and make minimumPointsDistance able to be set at a specific value uniformly for all figures, producing reduced versions of uniform quality, normalisation is used, and now minimumPointsDistance "internally" refers to the actual size of the printed-on-paper figure.

You can download the modified matlab2tikz.m file from here along with instructions.

NOTE that exhaustive tests were NOT made so be careful!