Insert Mathlab figure in Latex

8 views (last 30 days)
I have been unable to insert Matlab figure in my LaTex document and I am not sure what the problem is.
I usually use the code below to create the document in MATLAB.
print -depsc Nocostfunction41.eps
\begin {figure}[H]
\includegraphics[scale =0.4]{Simulation Figures/Nocostfunction41}\\
\caption{Figure 1}
I have attached a screenshot of the error. I am using BigSur OS and TexShop, MATLAB R2021
Has anyone experience this type of problem

Accepted Answer

Alberto Cuadra Lara
Alberto Cuadra Lara on 16 Dec 2021
Hello Telema,
It appears that the Latex compiler you are using could not find the file. Usually empty spaces give errors. Try renaming the folder and let's see.
By the way, I wrote a simple script for a colleague a few weeks ago to print custom graphics, save them, etc. It might be helpful.
I have changed the default save format to eps to match your case.
function myplot(x, y, varargin)
% Function that plot two given datasets x, y with same dimensions
% Examples:
% myplot(x, y)
% myplot(x, y, 'xlabel', '$\phi$')
% myplot(x, y, 'xlabel', '$\phi$', 'grid', 'on')
% Author: Alberto Cuadra
% Last update: 16 Dec 2021
% Inputs (optional)
xlabelname = [];
ylabelname = [];
customfilename = 'myfigure';
customfolderpath = [];
% Constants
Ncolors = length(x);
config = default_config;
% Colormap
colorbw = brewermap(Ncolors, 'Spectral');
% Check varargin inputs (Write custom definitions in case you want to
% have a more generalized routine)
% The input will be, e.g., myplot(x, y, 'xlabelname', xlabelname)
if nargin - 2 > 0
for i = 1:2:nargin-2
switch lower(varargin{i})
case {'xlabelname', 'xlabel'}
xlabelname = varargin{i + 1};
case {'ylabelname', 'ylabel'}
ylabelname = varargin{i + 1};
case {'filename', 'customfilename'}
customfilename = varargin{i + 1};
case 'grid'
config.grid = varargin{i + 1};
case 'saveformat'
config.saveformat = varargin{i + 1};
% Custom stuff ...
% Set up figure
[fig, axes] = generate_figure(config);
xlabel(axes, xlabelname, 'FontSize', config.fontsize, 'interpreter', 'latex');
ylabel(axes, ylabelname, 'FontSize', config.fontsize, 'interpreter', 'latex');
% Plot
for i=1:Ncolors
plot(x(i, :), y(i, :), 'color', colorbw(i, :), 'LineWidth', config.linewidth);
% Save figure
folderpath = strcat(pwd, customfolderpath, '\');
filename = customfilename;
saveas(fig, strcat(folderpath, filename), 'epsc');
function [fig, axes] = generate_figure(config)
% Function to generate a predefined figure
fig = figure;
set(fig,'units','normalized','innerposition',[0.05 0.05 0.9 0.9],...
'outerposition',[0.05 0.05 0.9 0.9]);
axes = gca;
set(axes,'LineWidth', config.linewidth, 'FontSize', config.fontsize-2, 'BoxStyle', config.boxstyle)
grid(axes, config.grid); box(axes,; hold(axes, config.hold); axes.Layer = config.layer;
function config = default_config
% Set default configuration parameters
config.fontsize = 22;
config.linewidth = 2;
config.grid = 'off'; = 'off';
config.hold = 'on';
config.layer = 'Top';
config.boxstyle = 'full';
config.saveformat = 'epsc';
  1 Comment
Telema Harry
Telema Harry on 16 Dec 2021
Thank you so much.
Removing the space in the folder solved the problem.

Sign in to comment.

More Answers (0)


Find more on Printing and Saving in Help Center and File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!