For exmaple,I have the following code.

% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.

n = 50;            % 50 data points
r = rand(n,1);

% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
hold off
title('Mean of Random Uniform Data')

for v = 1.0:-0.2:0.0

I want to convert MATLAB code to an image.

For example, if you copy the MATLAB code into a software then it return the image like this:

How to do it? If I use this code and publish it to PDF file,the code is not completely display.


How can I convert pdf to image


From the MATLAB editor you can Publish your document as a PDF (by changing the Output file format to pdf under publishing options). It will also evaluate the code unless you change the Publishing Options>Code Settings>Evaluate code to false.

The PDF can then can converted to an image (a quick google search gives an online PDF to JPG converter).

You can break a line and continue on the next with ..., for example


can be written as

sumLumi(x,y) = LLmap3(floor(x/4)+1, floor(y/4)+1) ...
               + LLmap3(floor(x/4)+2, floor(y/4)+1) ...
               + LLmap3(floor(x/4)+1, floor(y/4)+2) ...
               + LLmap3(floor(x/4)+2, floor(y/4)+2);

If you want to use Matlab only you can use this workflow:

  • Make your code to be single .m file. Any text file will work.
  • Read this file in cell column vector Code;
  • Process the lines adding using TeX or LaTeX formatting commands;
  • Create white figure with text(x,y,Code);
  • Export the figure.

This might be good way to produce many figures with same style.