Insert image while writting text in a file

8 views (last 30 days)
Michael Klein
Michael Klein on 8 Jul 2022
Answered: Harshit on 15 Sep 2023
I am currently writting a program that creates a worksheet (.mlx file) for my students. My program creates a different worksheet depending on the paramters that I give the program, so I can create different versions of the worksheet. I use fopen and fprintf to write text in my file (That text is depending on the given parameters).
Is there a way that I can also insert images into my .mlx file with the fprintf command or any other command?
The images I want to insert are .pdf files and plots that I created in the program that writes the worksheet file.
Thank you for your help!

Answers (1)

Harshit
Harshit on 15 Sep 2023
Hi,
I understand that you want to insert text and images inside a ".mlx" file programmatically.
Unfortunately, the ".mlx" file format does not support direct insertion of contents using commands like "fprintf".
In MATLAB's Live Editor, the content within an ".mlx" file is primarily authored using MATLAB code, formatted text, and output results. It does not provide direct support for executing external commands to modify the file content.
While you can insert text using commands in a ".m" file, this file format does not support images.
If you wish to include images or modify the content programmatically, you can consider using other file formats like HTML or PDF. These formats offer more flexibility for incorporating images and executing external commands to modify the content.
Here are some examples:
For HTML files:
fid = fopen('worksheet.html', 'w');
fprintf(fid, '<html>\n');
fprintf(fid, '<body>\n');
fprintf(fid, '<h1>Worksheet</h1>\n');
fprintf(fid, '<img src="image1.jpg">\n');
fprintf(fid, '<img src="image2.jpg">\n');
fprintf(fid, '</body>\n');
fprintf(fid, '</html>\n');
fclose(fid);
For PDF files:
% Save MATLAB figures as PDF files
print(figure1, 'figure1.pdf', '-dpdf');
print(figure2, 'figure2.pdf', '-dpdf');
% Merge PDF files using external tools
system('pdftk figure1.pdf figure2.pdf cat output worksheet.pdf');
Please note that using HTML or PDF files will not provide the same level of direct editing and execution capabilities as the MATLAB Live Editor.
Hope this helps!
Regards,
Harshit

Categories

Find more on Scripts in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!