How to embed images with variable root folders to HTML reports?
9 次查看（过去 30 天）
Hi MATLAB community,
I've got a script producing a html report with text and images. The images have been created in MATLAB (as figures), and have been exported as .png files before being read into the report with the following code:
fprintf(fidHTML, '%s\r\n', ['<img src="Figures/JointProbabilities.png','" alt="HTML5 Icon">']);
Where 'fidHTML' is the report. This worked when the path to the images in consistent.
However, now I have changed the code so that the images are saved in a folder dependent on variable names, so the path to the images depends on the variable names and the specific run. The path to the images now is as follows:
Filepath = [outputDir,'/','Summary_Figures/',inputFile.name,'/',saveName,'/JointProbabilities.png']
I have tried a few ways but incorporate these variable names and the new path into the "<img src = ...." line of code above but haven't been able to figure it out. Can you use variable names like this when adding images to html reports? If not, what is an alternative?
As an example, I don't understand why something like:
fprintf(fidHTML, '%s\r\n', ['<img src="Filepath','" alt="HTML5 Icon">']);
Doesn't work, or why this functionality doesn't exist?
Any suggestions appreciated!
Suman Sahu 2023-3-10
编辑：Suman Sahu 2023-3-10
The reason why the line of code you provided doesn't work is because you are passing the string "Filepath" instead of the Filepath variable's actual value.
Here is an example of code that shows how you can achieve that:
%create the full path to the image file
filepath = fullfile(outputDir, 'Summary_Figures', inputFile.name, saveName, 'JointProbabilities.png');
%pass the path as placeholder
fprintf(fidHTML, '<img src="%s" alt="HTML5 Icon">\n', filepath);
You can read more about the fullfile function from here: Build full file name from parts - MATLAB fullfile - MathWorks
Hope it helps.