background of editable cells UITABLE

22 views (last 30 days)
Reeny
Reeny on 29 Jul 2019
Edited: Reeny on 1 Aug 2019
Hi !
I created an uitable with guide and I would like to change the background of editable cells.
I tried to use the HTML code with the method https://www.mathworks.com/matlabcentral/answers/25038-how-to-change-each-cell-color-in-a-uitable , it works for the background, but I can't change the value of my cell anymore.
Any ideas ?
  12 Comments
Reeny
Reeny on 29 Jul 2019
I put a break at the line row = eventdata.Indices(1).
Each next line is executed correctly. But, when the if is over, the first line (row = eventdata.Indices(1)) is executes once again. I don't understand why.
The full copy-pasted error message is :
Index exceeds matrix dimensions.
Error in Table>uitable1_CellSelectionCallback (line 131)
row = eventdata.Indices(1)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Table (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Table('uitable1_CellSelectionCallback',hObject,eventdata,guidata(hObject))
Error while evaluating Table CellSelectionCallback
Adam Danz
Adam Danz on 29 Jul 2019
Bingo! See answer. Let me know how that turns out.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 29 Jul 2019
Edited: Adam Danz on 29 Jul 2019
[Continued from comments section under the question]
Summary of the problem
"when the [callback function] is over, the first line is executes once again. I don't understand why."
This is an unfortunate, known problem in GUIDE GUIs. The 2nd invocation of the callback function is the result of changing the object focus which is inevitable.
Solution
To get around this, at the top of your callback function write a conditional that escapes the callback function if the indices are empty.
if isempty(eventdata.Indices)
return
end
  15 Comments
Adam Danz
Adam Danz on 1 Aug 2019
Nice work! In your HTML version, you were changing the color of single cells. BackgroundColor property changes the color of the entire row. If this is what you want to do it's definitly a better choice than HTML. Thanks for providng the additional answer!
Reeny
Reeny on 1 Aug 2019
My first idea was to change the color of the selected cell only, but with HTML it was too difficult... At least, with this solution, I can edit my cells without difficulties :)

Sign in to comment.

More Answers (1)

Reeny
Reeny on 1 Aug 2019
Edited: madhan ravi on 1 Aug 2019
If you want to change the background of a selected row, you can use the « Background Color » property.
In my case, I have two columns: one with numbers or text and the second with checkboxes. I defined my data in the Table Property Inspector.
I wanted to change the background of the row that I selected. Here is my code in the CellSelectionCallback function:
if isempty(eventdata.Indices)
return
end
row = eventdata.Indices(1);
column = eventdata.Indices(2);
if column == 1
bgColor = [1 1 1];
for i = 1:4 %4 = my number of row
bgColor = [bgColor ; 1 1 1]; %Defines a white background for all rows
end
bgColor(row, :) = [0, 0.4470, 0.7410]; %Changes the color of background at the selected row
set(handles.uitable1, 'BackgroundColor', bgColor, 'ColumnEditable', [true, true]);

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!