Show Sidebar Hide Sidebar

LaTeX in MATLAB

How to add LaTeX to MATLAB graphs.

% Calculate the Fibonacci numbers from 1 to 12
fib = zeros(1, 12);
for i = 1:12
    fib(i) = (((1+sqrt(5))/2)^i - ((1-sqrt(5))/2)^i)/sqrt(5);
end

% Plot the first 12 Fibonacci numbers
figure
plot(1:12, fib, 'k^-')

% Add a title and axis labels
title('Fibonacci Numbers from 1-12')
xlabel('n')
ylabel('F_n')

% Build a string that contains the Latex expression
eqtext = '$$F_n={1 \over \sqrt{5}}';
eqtext = [eqtext '\left[\left({1+\sqrt{5}\over 2}\right)^n -'];
eqtext = [eqtext '\left({1-\sqrt{5}\over 2}\right)^n\right]$$'];

% Add the string containing the Latex expression to the plot
text(0.5, 125, eqtext, 'Interpreter', 'Latex', 'FontSize', 12, 'Color', 'k')

% Send to Plotly!
fig2plotly()
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', [1, 2, 3, 4], ...
  'y', [1, 4, 9, 16], ...
  'name', '$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$', ...
  'type', 'scatter');
trace2 = struct(...
  'x', [1, 2, 3, 4], ...
  'y', [0.5, 2, 4.5, 8], ...
  'name', '$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$', ...
  'type', 'scatter');
data = {trace1, trace2};
layout = struct(...
    'xaxis', struct('title', '$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$'), ...
    'yaxis', struct('title', '$d, r \text{ (solar radius)}$'));
response = plotly(data, struct('layout', layout, 'filename', 'latex', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.