How to save a uitable as either a png, jpeg, pdf, excel?

Hi!
I have uitable that I created that changes the background color of the data row depending if certain conditions are met. I am now trying to export that out of matlab and save it as either a png, jpeg, pdf, excel table (I'm not picky). It is very important to maintain the background color of the table rows as it is what gives them utility. I'm not sure how to go about this, working with R2019b.

Answers (2)

saveas() or print() the figure .
You might be able to create a new figure and copyobj() the uitable into it, and saveas() or print() that figure -- thereby ignoring everything else that is in the original figure. export_fig() from the File Exchange might be willing to do that on your behalf.

4 Comments

Hi Walter, thanks for the quick reply!
I've been trying to save using saveas or print, but I either get an error when trying to pass the figure (since it is a uifigure) or it saves a blank image file or pdf. Any suggestions on how to fix that? Here's my code below:
fig = uifigure;
uit = uitable(fig);
uit.Data = RSTable;
for ifig = 1:size(RSTable)
if RSTable.Value(fig)>0
s = uistyle('BackgroundColor', 'red');
addStyle(uit,s,'row',ifig);
else
s = uistyle('BackgroundColor', 'green');
addStyle(uit,s,'row',ifig);
end
end
Ah I did not realize that it was app designer. There does not appear to be any supported way of doing that for uifigure!
Someone posted a script to automatically recreate parts of a plot as traditional figure. However you speak about cell coloration as being the key to your plot, and cell coloration in uifigure works very differently in traditional figures in a way that would be tricky to automate I suspect.
Ah dang ok, it was so easy to do through the uifigure function! I'm not necessarily tied to using app designer, is there another way to obtain the same functionality with a traditional table?
You can color individual cells in uitable() of traditional figures by using a trick.
In order to color an individual cell, you must set your Data property to a 2D cell array of values. For any column in which even one entry is to be colored, the entire column must be converted to character vectors, even if you are trying to show numeric values. For example if you were trying to do the traditional weather reports showing temperatures with red color for the daily high and blue for the daily low, then the entire column would have too be converted to character vector.
Once you have the column in character vector for, in order to color any one entry, you must convert it into HTML 1.1 format, independently of all of the other entries in the row, by putting in <html> at the beginning of the character vector. You can omit the </html> at the end of the vector, though. Then you can include HTML coloring and HTML character entities such as <TD bgcolor="red">-11.2&deg;C</TD>
Remember, this <html> prefix must be put independently into each character vector.
Technically it is possible to use HTML <TABLE> inside a cell, and even to put multiple table entries including using TR and TD. The problem with this is that the renderer assumes that you only have a single normal line, so sizing the entries properly is difficult to impossible if you do that. You can even use HTML image insertion, which is a pain to get right but it does work.

Sign in to comment.

Hi Camicat,
I understand that you are trying to export a UITable along with the background colour of the cells. Here is how you can export the UITable to an Excel file, inspired by this answer.
Pull the data from the table using “get(uit,'Data')” and save it to an Excel file using the “writematrix” function. Then, in a loop, for each style configuration (which includes the background colours added using “addStyle”) of the table, apply it to the Excel workbook. You can get all the style configurations of the table using the “get(uit,'StyleConfigurations')” command. To set a colour to an Excel sheet cell, I followed the guide given here:
Now you have the UITable’s data and cell background colours exported to an Excel file.
For your reference, I am attaching my code to do this. Here’s a comparison of the UITable and the exported Excel file:
I hope this could help!

Asked:

on 28 Feb 2020

Answered:

on 6 Sep 2024

Community Treasure Hunt

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

Start Hunting!