export a uitable with backgound color

Hi all,
I met a problem when I want to export my uitable where some cells have their own background color.
I tried
>> exportapp(fig, 'my_uitable.jpg');
>> exportapp(fig, 'my_uitable.pdf');
but unfortunately, it is a capture of the screen. My uitable has about 256 rows.
because
thanks for the function uitable2html, but it does not work for me.
I tried (chatgpt)
WHAT I WANT is just to export the uitable with the backgound color, to a pdf, a html, etc.
Here is some of my codes to create a uitable.
fig = uifigure;
uit = uitable(fig,'Data',subregions);
for iSubregion = 1:length(subreg)
for iid = 1:height(pvalues_subreg)
if strcmpi(strcat('Region_', subreg{iSubregion}), pvalues_subreg.Var1{iid})
if any(pvalues_subreg.pvalues(iid) < palpha/(nsub+5))
s = uistyle('BackgroundColor',[1 0.8 0.8]);
addStyle(uit,s,'cell',[iSubregion,1]);
break;
end
end
end
end
Thanks a lot in advance for your help!!!

4 件のコメント

Voss
Voss 2023 年 10 月 3 日
移動済み: Voss 2023 年 10 月 4 日
"Warning: UI components will not be included in the output. To include UI components, use the 'exportapp' function."
Did you try using the exportapp function, as the warning suggests?
When I run this simple example
fig = uifigure();
uit = uitable(fig,'Units','normalized','Position',[0 0 1 1],'Data',magic(5));
s = uistyle('BackgroundColor',[1 0.8 0.8]);
addStyle(uit,s,'cell',[3,1]);
then save the uifigure (with uitable) to pdf
exportapp(fig,'test.pdf');
it works for me and generates the attached pdf file.
Jingwen Sun
Jingwen Sun 2023 年 10 月 4 日
移動済み: Voss 2023 年 10 月 4 日
thank you for your response, I tried the exportapp function, there is no warning. But it is just a capture of the screen. When the uitable has more than 100 rows, the exportapp function doesn't export all the rows?
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 4 日
To clarify, do you want to export the uifigure or the uitable?
Jingwen Sun
Jingwen Sun 2023 年 10 月 4 日
hello, I want to export the uitable with background color.

サインインしてコメントする。

回答 (2 件)

Rahul
Rahul 2024 年 9 月 6 日

0 投票

I understand that you wish to export a 'uitable' with some styling like background color. You wish to obtain the table as a ',pdf' file or '.html' file etc.
One of the workarounds you can use to achieve this is mentioned below:
In this workaround you can create a '.png' file initially and then you can use the '.png' file to create the '.pdf' or '.html' files.
You can add this code to your script to your existing code in order to obtain the 'uitable' as a '.png' file
% Capture the figure as an image
frame = getframe(fig);
img = frame.cdata;
% Save the image
imwrite(img, 'uitable_image.png');
Now using the code mentioned below, you can obtain a '.pdf' file from the '.png' file created before
import mlreportgen.dom.*;
doc = Document('my_uitable', 'pdf');
append(doc, Image('uitable_image.png'));
close(doc);
Now to obtain a '.html' file from the '.png' file created, you can follow this code:
htmlFile = fopen('my_uitable.html', 'w');
fprintf(htmlFile, '<html><body><img src="uitable_image.png" alt="UITABLE"></body></html>');
fclose(htmlFile);
Functions like 'imwrite', 'Document', 'fprintf' help in achieving the desired result of exporting the 'uitable' element to a '.png', '.pdf', '.html' file respectively.
Note: The 'uitable2html' function that you are trying to use is only able to convert numerical data and is provided by a 3rd party: https://fr.mathworks.com/matlabcentral/fileexchange/58438-export-uitable-to-html?s_tid=srchtitle_support_results_4_uitable%2520export
You can refer to the following Mathworks documentations to know more about these functions:
Hope this helps! Thanks.
Sreeram
Sreeram 2024 年 9 月 6 日

0 投票

Hi Jingwen,
I understand that you are trying to export a UITable along with the background colour of the cells. Although I could not find any direct method to do that, I can suggest a workaround to achieve this, 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:
You can programmatically save this Excel file as a PDF. For the details on how to print it, you can see this guide:
I hope this could help!

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品

リリース

R2022a

タグ

質問済み:

2023 年 10 月 3 日

回答済み:

2024 年 9 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by