How can I export the ANOVA results as a PNG?

9 views (last 30 days)
Hi,
I am doing an ANOVA (similar to the one in the example page) and I was wondering if there is a way to export that information that you get as a png?
Since I have to publish the results of the anova it would be super if I could just generate a picutre.
Thank you for you help.
  1 Comment
Moritz Malina
Moritz Malina on 6 Sep 2020
I am using the example from here: https://www.mathworks.com/help/stats/anovan.html
I just want the table which is shown in the field on the right.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 6 Sep 2020
Edited: Adam Danz on 30 Apr 2021
> How can I export the ANOVA results as a PNG?
If you're refering to the boxplot results, to save a figure as png, saveas(fig,filename,'png') where fig is the figure handle and filename is the name or, better yet, the full path (using fullfile) of the png output file. Get the figure handle using gcf.
If you're refering to the ANOVA table displayed in the figure, you woudn't want to insert that into a document as an image. It would be much better to save the table variable to a text file. Example:
[p, tbl, stats] = anova1(___);
writecell(tbl,'anovaresults.txt') % better to use full path
The txt file will be comma separated and will not maintain the column-aligned formatting. However, it will be easy to read back into Matlab using
tbl = readcell('anovaresults.txt');
Update If you're using Matlab R20201a or later, you can use formattedDisplayText to convert the table to a string that maintains its format which is demonstrated in this community highlight. The string can then be written to a text file using fprintf or another text writing function.
If the text file is going to be read back into Matlab, the first option is best. If the file is meant for human eyes, the formattedDisplayText option is best.
[p, tbl, stats] = anova1(___);
% Convert table to string
tblStr = formattedDisplayText(tbl);
% Write string to file
fid = fopen('anovaTable.txt', 'wt');
fileCleanup = onCleanup(@()fclose(fid));
formatSpec = '%s\n';
fprintf(fid, formatSpec, tblStr);
clear('fileCleanup')
Sample of results:
  4 Comments
Moritz Malina
Moritz Malina on 6 Sep 2020
Thank you again for your answer.
I cant get the saveas to work. I keep getting this error:
Unrecognized function or variable 'fig'.
Adam Danz
Adam Danz on 6 Sep 2020
Edited: Adam Danz on 10 Sep 2020
From my answer, "where fig is the figure handle". If you don't have the figure handle, use gcf() assuming the figure is the current figure.

Sign in to comment.


Image Analyst
Image Analyst on 30 Apr 2021
If you have r2020a or later, try exportgraphics().

Tags

Community Treasure Hunt

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

Start Hunting!