Clear Filters
Clear Filters

Color Figure Table Cell.

4 views (last 30 days)
Javier Imaz Higuera
Javier Imaz Higuera on 18 Jan 2024
I am trying to change the background color of the cells with a p-value < 0.05. The callback function is not working.
function plotBoxplotBDD(paramEMG, titulo, agrup, grupos, nom_unidades,parametros)
for p = 1:8
fig = figure;
fig.WindowState='maximized';
% titulo de la gráfica y disposición
h= tiledlayout(fig,6,2);
h.Title.String =parametros{p};
h.Title.FontSize = 24;
h.TileSpacing = 'loose';
h.Padding = 'compact';
% Colores de los diagramas
x=1:size(grupos,2);
nombre_grup=["Suprahyoids muscles" "Infrahyoids muscles"];
for a=1:2
nexttile(a,[5,1]);
for g=1:size(grupos,2)
datos = extractfield(paramEMG.(parametros{p}).(grupos{g}),['Med_' agrup{a}]);
hold on;
box=boxchart(x(g).*ones(size(datos)),datos);%, 'BoxFaceColor',C(g,:));
try
box.MarkerColor=box.BoxFaceColor;
end
end
xticklabels([]);
xlabel('');
ax = gca; ax.FontSize=14; ax.YGrid = 'on'; ax.XAxis.Visible = 'off';
title(nombre_grup(a),'VerticalAlignment','top','FontSize',19);
if a ==1
limF1 = ax.YLim;
ylabel(nom_unidades(p), 'FontSize', 20);
else
ax.YLim=[min([ax.YLim,limF1]) max([ax.YLim,limF1])];
yticklabels([])
ylabel('');
end
end
lgd = legend(grupos, 'FontSize', 15);
lgd.Layout.Tile = 'north';
lgd.NumColumns = round(size(grupos,2)/2);
lgd.Orientation = 'horizontal';
% Añadir tabla estadística
% Crear un axes en la ubicación deseada
ax = nexttile(11,[1 2]);
% Crear una uitable y colocarla en la figura, no en el axes
uit = uitable(gcf);
% Configurar la propiedad 'Units' de la uitable a 'normalized' para que se redimensione con el axes
uit.Units = 'normalized';
% Configurar la posición de la uitable para que parezca que está dentro del axes
% Los valores son [izquierda, abajo, ancho, alto]
drawnow()
uit.Position = [ax.Position(1), ax.Position(2), ax.Position(3), ax.Position(4)];
% Supongamos que 't' es tu tabla
t = paramEMG.(parametros{p}).ComparacionMedianasGrupos;
% Convertir la tabla a una matriz de celdas
cellData = table2cell(t);
% Crear una nueva matriz de celdas para almacenar los p-valores
pValueCellData = cell(size(cellData));
% Iterar sobre las celdas de cellData
for i = 1:size(cellData, 1)
for j = 1:size(cellData, 2)
% Seleccionar solo el p-valor (el primer valor de cada celda)
pValueCellData{i, j} = cellData{i, j}(1);
end
end
% Añadir los nombres de las variables de la tabla como la primera fila
% pValueCellData = [t.Properties.VariableNames; pValueCellData];
% Ahora puedes asignar pValueCellData a la propiedad Data de uitable
uit.Data = pValueCellData;
uit.ColumnName=t.Properties.VariableNames;
uit.RowName=agrup;
% Obtén el ancho de la posición
drawnow()
anchoPosicion = fig.Position(3)-225;
% Calcula el ancho de cada columna
anchoColumna = anchoPosicion / width(t);
% Ajusta el ancho de las columnas
uit.ColumnWidth = repmat({anchoColumna}, 1, width(t));
% ax.XColor=[];
ax.XTick=[];ax.YTick=[]; ax.XAxis.Visible = 'off';ax.YAxis.Visible = 'off';ax.Color='none';
pause(0.1);uit.Position(4)=uit.Extent(4);uit.Position(3)=uit.Extent(3);
% Crear una función de callback para el evento CellEditCallback
uit.ColumnEditable = true;
uit.CellEditCallback = @(src, event) colorCells(src, event);
end
end
function colorCells(src, event)
% Obtener los datos de la tabla
data = src.Data;
% Iterar sobre cada celda en los datos
for i = 1:size(data, 1)
for j = 1:size(data, 2)
% Si el valor de la celda es menor a 0.05
if data{i, j} < 0.05
% Colorear la celda de rojo
src.BackgroundColor(i, j, :) = [1 0 0];
else
% De lo contrario, dejar la celda en blanco
src.BackgroundColor(i, j, :) = [1 1 1];
end
end
end
end

Answers (1)

Muskan
Muskan on 22 Jan 2024
Hi Javier,
You can use the below method of coloring a cell of uitable based on the value of the cell:
I hope this helps!
  1 Comment
Javier Imaz Higuera
Javier Imaz Higuera on 23 Jan 2024
the problem is that "Styles are only supported when the table UI component is in a figure created with the uifigure function" and I want to work with a figure.
The problem is that I cannot use the uifigure with the tiledlayout functionality.
I thought the solution was working with uit.CellEditCallback = @(src, event) colorCells(src, event);
However, while debugging, the code just jumps over it.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!