Using Matlab to create a LaTex document (for image import)

34 vues (au cours des 30 derniers jours)
Niklas Kurz
Niklas Kurz le 27 Avr 2022
Commenté : Niklas Kurz le 28 Avr 2022
In order to import multiple figures in LaTex from one folder I have found following code:
fileID = fopen('./incl_img_latex.txt', 'w');
files = dir('path');
for file = files'
str = file.name;
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \n', str);
end
fclose(fileID);
This will create for each file some LaTeX code:
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0252.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0253.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0254.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0255.jpg}}
\end{figure}
Now here comes the tricky thing that I want to implement: For each second file the Latex code should vary:
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \\clearpage \n \n', str);
So something like that is received:
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0252.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0253.jpg}}
\end{figure}
\clearpage %this Line is new
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0254.jpg}}
\end{figure}
\begin{figure}[htb!]
\centering
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{1pt}
\fbox{\includegraphics[width = 0.95\textwidth]{IMG_0255.jpg}}
\end{figure}
\clearpage %this Line is new
How do I achieve this?

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Avr 2022
Modifié(e) : Walter Roberson le 27 Avr 2022
fileID = fopen('./incl_img_latex.txt', 'w');
files = dir('path');
files([files.isfolder]) = []; %remove . and .. and any directories
for fileidx = 1 : numel(files)
file = files(fileidx);
str = file.name;
fprintf(fileID, '\\begin{figure}[htb!]\n\\centering\n\\setlength{\\fboxsep}{1pt}\n\\setlength{\\fboxrule}{1pt}\n\\fbox{\\includegraphics[width = 0.95\\textwidth]{%s}}\n\\caption{}\\end{figure}\n \n', str);
if mod(fileidx,2) == 0
fprintf(fileID, '\\clearpage\n');
end
end
fclose(fileID);

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by