Borrar filtros
Borrar filtros

How to embed images with variable root folders to HTML reports?

20 visualizaciones (últimos 30 días)
Ben
Ben el 2 de Mzo. de 2023
Respondida: Ben el 10 de Mzo. de 2023
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!
Thanks :)

Respuesta aceptada

Suman Sahu
Suman Sahu el 10 de Mzo. de 2023
Editada: Suman Sahu el 10 de Mzo. de 2023
Hi Ben,
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.
  1 comentario
Ben
Ben el 10 de Mzo. de 2023
Thanks Suman! I also got a successful solution from ChatGPT that I will also post here

Iniciar sesión para comentar.

Más respuestas (1)

Ben
Ben el 10 de Mzo. de 2023
Hi everyone, I managed to get a successful answer from ChatGPT that is similar to Suman's answer. The full answer is below:
"When you include a path to an image in your HTML code, the path needs to be a string that points to the location of the image file. In your case, since the path to the image depends on variables, you can create the path as a string and then insert it into the HTML code using string concatenation.
Here is an example of how you can do this:
filepath = [outputDir,'/','Summary_Figures/',inputFile.name,'/',saveName,'/JointProbabilities.png'];
fprintf(fidHTML, '<img src="%s" alt="HTML5 Icon">\n', filepath);
In this example, filepath is created by concatenating the output directory, "Summary_Figures", the input file name, the save name, and the image file name. Then, the fprintf statement uses string interpolation to insert the filepath string into the HTML code.
Using 'Filepath' in the fprintf statement wouldn't work because that would literally insert the string "Filepath" into the HTML code, rather than the value of the filepath variable.
By using string interpolation, you can create a dynamic path to the image that depends on the specific variables used in each run."

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by