Azzera filtri
Azzera filtri

export a uitable with backgound color

44 visualizzazioni (ultimi 30 giorni)
Jingwen Sun
Jingwen Sun il 3 Ott 2023
Risposto: Sreeram il 6 Set 2024 alle 10:59
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 Commenti
Dyuman Joshi
Dyuman Joshi il 4 Ott 2023
To clarify, do you want to export the uifigure or the uitable?
Jingwen Sun
Jingwen Sun il 4 Ott 2023
hello, I want to export the uitable with background color.

Accedi per commentare.

Risposte (2)

Rahul
Rahul il 6 Set 2024 alle 6:53
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 il 6 Set 2024 alle 10:59
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!

Categorie

Scopri di più su Develop uifigure-Based Apps in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by