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

23 次查看(过去 30 天)
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.

回答(2 个)

Walter Roberson
Walter Roberson 2020-2-28
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 个评论
camicat
camicat 2020-3-2
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?
Walter Roberson
Walter Roberson 2020-3-2
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.

请先登录,再进行评论。


Sreeram
Sreeram 2024-9-6
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!

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by