How to add captions to figures in a LiveScript that also include figure numbers

25 views (last 30 days)
Hello, I am using Live Editor to create a professional report that can be automatically generated with data. I am attempting to add captions to the figures, and have been unsuccessful at:
  • Center-justifying the caption
  • Reducing the caption font size
As an example, I have inserted a figure manually, and directly below that I have this code:
figureSpec = "Figure %d: Results vs. Time";
figureNumber = figureNumber+1;
strjust(fprintf(figureSpec,figureNumber),'center');
"figureNumber" has been established at the beginning of the LiveScript.
The overall objective here is to have figure numbers that will auto-update if I happen to remove a prior figure, or include extra figures. It doesn't seem to be an option to add figure numbers to captions, or even captions to figures for that matter. Any suggestions would be great, thanks!
  2 Comments
Kevin Holly
Kevin Holly on 21 Jan 2022
Here are a few ideas:
Is your figure on a single axes? If so, you could try
xlabel(['Figure ' num2str(figureNumber) ': Results vs. Time'])
You could also manually insert spaces
figureSpec = " Figure %d: Results vs. Time";
or tabs
figureSpec = "\t\t\tFigure %d: Results vs. Time";
Or alternatively, you could use the report generator.
Cory Dinkle
Cory Dinkle on 21 Jan 2022
Thanks, these figures are already created, and in some cases they are not Matlab figures but simple schematics, therefore the first option would not work unfortunately. The second and third suggestions may do what I need, though I think I may be spending too much time trying to automate such a simple function (figure numbering).
Finally, yes the report generator would definitely do this, but I don't think I am going to put in a request to HR for a $1k add-on that is much less capable than simply writing in LaTeX and importing the figures and data that way. Thank you for the suggestions!

Sign in to comment.

Answers (1)

Ankit
Ankit on 21 Jan 2022
Edited: Ankit on 21 Jan 2022
@Cory Dinkle.. I would recommend if you can use annotations. With annotations it is easy to change different properties like alignment, font size, name etc very easily. I hope this could help you to generate your report as expected.
close all;
figure;plot(rand(1,5));
xlabel('X-Axis');ylabel('Y-Axis')
captionFigure()
figure;
data = [2 4 6 7 8 7 5 2];
stem(data)
captionFigure()
figure;plot(1:10);
captionFigure()
function captionFigure()
figList = sort(get(0,'Children'));
i = length(figList);
figureSpec = {['Figure: ' num2str(i) ' Results vs. Time']};
dim = [0.1, 1.0, 0.8, 0.0];
a = annotation('textbox', dim, 'String', figureSpec, 'FitBoxToText', 'on', 'LineStyle', 'none');
a.Color = 'red';
a.FontSize = 12;
a.HorizontalAlignment = 'center';
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!