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

38 ビュー (過去 30 日間)
Niklas Kurz
Niklas Kurz 2022 年 4 月 27 日
コメント済み: Niklas Kurz 2022 年 4 月 28 日
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?

採用された回答

Walter Roberson
Walter Roberson 2022 年 4 月 27 日
編集済み: Walter Roberson 2022 年 4 月 27 日
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);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Report Generator についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by