MATLAB Answers

Adding Color to Text in Rows in UI Table in GUI

10 views (last 30 days)
hamzah khan
hamzah khan on 26 Jul 2020
Commented: hamzah khan on 29 Jul 2020
Hello,
I am trying to add color to text in the UI Table, here is my code. It starts by the user loading the files and then displaying the file names in the table. How can I add different color to each file name?
AssemblyData= {'Name1',0, 0};
AssemblyTable= uitable(f, 'Units', 'centimeters','Position', [1,1,7,5], 'Data', AssemblyData,'ColumnEditable',[false, true, true], 'ColumnName',{'Name'; 'Offset[m]';'MeshSize[m]'},'CellEditCallback', {@AssemblyEdit_Callback});
function AddBody_Callback(source,eventdata)
[files,pathToFiles] = uigetfile('*.mat',...
'Select One or More Files', ...
'MultiSelect', 'on');
[BodyDataAdd,k, FileNames] = BodyDataLoad(files,pathToFiles);
if isempty(BodyData)
BodyData= BodyDataAdd;
else
BodyData= [BodyData; BodyDataAdd];
end
for TempIndex= 1:k % k is the number of files added
if k==1
AssemblyData{BodyNumber+TempIndex,1} = FileNames;
else
AssemblyData{BodyNumber+TempIndex,1} = FileNames{TempIndex};
end
AssemblyData{BodyNumber+TempIndex,2} = 0;
AssemblyData{BodyNumber+TempIndex,3} = 0;
end
BodyNumber= BodyNumber+k;
AssemblyTable.Data= AssemblyData;
end

  0 Comments

Sign in to comment.

Answers (2)

Andreas Bernatzky
Andreas Bernatzky on 27 Jul 2020
Hi if you have an handle h to the object you can try this:
set(h, 'Color',[1, 0 ,0], 'FontSize', 20)

  3 Comments

hamzah khan
hamzah khan on 27 Jul 2020
I am trying to do this,
for up =1:k
set(AssemblyTable{up,:},'ForegroundColor',b(up,: ));
end
Since my table has 3 columns, I want to give color to rows. I am getting this error
"Brace indexing is not supported for variables of this type."
hamzah khan
hamzah khan on 28 Jul 2020
Hi,
I have changed my loop,
now the error I am getting is
"Index in position 1 exceeds array bounds (must not exceed 1)."
Although k is 2 and Assembly Table also has two rows
for up =1:k
set(AssemblyTable(up,1),'ForegroundColor',b(up,: ))
endfor up =1:k
set(AssemblyTable(up,1),'ForegroundColor',b(up,: ))
end

Sign in to comment.


Walter Roberson
Walter Roberson on 29 Jul 2020
We cannot be sure from what you posted whether your uitable is on a traditional figure or on a uifigure . The style of your code would be more consistent with it being a traditional figure.
When you use uitable on tradtional figures, you are limited to setting foreground and background that affect all of the rows, and to set "row striping" that affects alternate rows. https://www.mathworks.com/help/matlab/ref/matlab.ui.control.table-properties.html#buiu91u-1-RowStriping .
However if you use uitable on traditional figures, and you make all of the cells into cell array of character vectors, then you can put HTML into each one individually, making each one a separate <table> that you can then set bgcolor for... one cell at a time. You would not want to make such cells editable as the user would be editing the HTML.
If you use uitable on uifigure, then new style facilities become available; see https://www.mathworks.com/help/matlab/ref/matlab.ui.style.style-properties.html

  1 Comment

hamzah khan
hamzah khan on 29 Jul 2020
Thanks
I figured out that changing the background color is also working for me :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!