Not able to view legend in Axes handle using report generator toolbox

8 views (last 30 days)
Hello Folks,
I am facing an issue using Axes handle in Report Generator toolbox.
First of all I am generating "axis handle" in MATLAB Application GUI. This is being used as input to one of the function in takes this axis handle and converts to report dom compliable "Axes Handle".
My problem is that in "axis handle" as well as "Axes Handle" in both this I am able to view their legends in properties stored. When I am appending it to a report these legend are not visibile.
This is a small part of code which I am using.
% Function used to generate report
GenerateReport(vargain)
% Inside the function
axh1 = Axes(varargin{1});
append(ch1,axh1)
append(rpt,ch1)
Here are the sample figures.
This is the "axis handle" which I am able to see when this figure is generated.
And this is the figure which is being seen in report. This is the snapshot of report generated.
This is the properties which I am able to see in "axh1.Source.Legend.String"
If you see in the ablove figure these legend are stored in String format in "Axes Handle" properties.
My problem is that how to add these legends in the "Axes Handle" ?
Any ideas would be appriciated.
Thanks in Advance.

Accepted Answer

Jay
Jay on 26 Oct 2023
Edited: Jay on 26 Oct 2023
Just an Update,
I had contacted to MATWORKS Support and I shared this problem with them as well. To my surprise this is a error and we can't plot legends on figure using axis handle only. They will try to work on this error and get updates on next release may be.
But I found a workaround soultion from one of the similar questions asked. Here is the link to that question. How can I save UIAxes as an image when button is pushed in app designer? - MATLAB Answers - MATLAB Central (mathworks.com)
The solution is that the person who answered to this question has created a function called as copyUIAxes(). This solution will only work for the Axes coming from GUI as UIAxes having 'matlab.ui.control.UIAxes' class. This will create a struct containing axes, figure, legend classes within.
For me this solution has worked but lets hope we can get update from mathworks as well on future release.

More Answers (1)

Yash
Yash on 20 Oct 2023
Hi Jay,
I understand that you are unable to view the legend of a figure when it is added to a report generated using the "Report Generator" toolbox. Instead of appending the "Axes Handle" to the "Chapter" object, set the legend of the plot with the values "axh1.Source.Legend.String" using the "legend()" function and append the plot to the chapter. Please refer to the documentation provided below for more information on "legend()" function: https://www.mathworks.com/help/releases/R2021b/matlab/ref/legend.html. Consider updating the code in the following manner:
% Inside the function
axh1 = Axes(varargin{1});
% figure plotting
fig = figure(gcf);
ax = gca;
% xData is x axis data of size 1xN
% yData is y axis data of size pxN
% N is number of data points
% p is number of plots
plot(ax, xData, yData);
% set legend
legend(axh1.Source.Legend.String);
append(ch1,fig); % fig is the figure window
append(rpt,ch1);
Alternatively, generate the plot and save it as a image and add the image to the report
% Inside the function
fig = figure(gcf);
ax = gca;
x = linspace(0,5);
y1 = sin(x/2);
y2 = cos(x/2);
y = [y1;y2];
% generate plot
plot(ax,x,y);
str = ['sin(x/2)';'cos(x/2)'];
legend(str);
% save plot as image
saveas(gcf,"myPlot_img.png");
plot1 = Image("myPlot_img.png");
% append the image to chapter
append(ch,plot1);
% append the chapter to report
append(rpt,ch);
I hope this helps!
  1 Comment
Jay
Jay on 22 Oct 2023
Hello @Yash
Thankyou for your answer.
My issue a bit diffrent. I have created a MATLAB GUI using "Application Builder", Where in this I am using report genrtor library at end of simulation to compile a report for all the figures generated.
According to your first solution, I am already adding legend to my figure keeping, axes handle as my first input. So when we look at the "axis handle" property inspector, There are mainly two handles in Figure handle names as "axis handle (Figure name)" & "legend". See fig below.
As you can see Legend is diffrent then axis handle. And I am appending legend in axis handle in my function already. And it has all the data of legend I want. So when converting "axis handle" to "Axes Handle" for report generating library, It takes just this "axis handle", without taking legend into account. But I can see the legend is stored in this handle well but just in String property. So, there I dont know how to work it around.
I cant append figure handle to report as I have just axis handle to work with. Figure handle is out of the question for me.
According to your second solution, This application will be deployed on the Web server(A part of Application tool), where I storing of figures is not possible.
Lets try to work it around maybe we will find something. If I find a solution, I will post here as well.
Thankyou

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!