Insert Mathlab figure in Latex

2 views (last 30 days)
Hi,
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]
\centering
\includegraphics[scale =0.4]{Simulation Figures/Nocostfunction41}\\
\caption{Figure 1}
\label{figure1}
\end{figure}
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.
Best,
Alberto.
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 ...
end
end
end
% 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);
end
% Save figure
folderpath = strcat(pwd, customfolderpath, '\');
filename = customfilename;
saveas(fig, strcat(folderpath, filename), 'epsc');
end
% SUB-PASS FUNCTIONS
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, config.box); hold(axes, config.hold); axes.Layer = config.layer;
end
function config = default_config
% Set default configuration parameters
config.fontsize = 22;
config.linewidth = 2;
config.grid = 'off';
config.box = 'off';
config.hold = 'on';
config.layer = 'Top';
config.boxstyle = 'full';
config.saveformat = 'epsc';
end
  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)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!