background of editable cells UITABLE

25 vues (au cours des 30 derniers jours)
Reeny
Reeny le 29 Juil 2019
Modifié(e) : Reeny le 1 Août 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 commentaires
Reeny
Reeny le 29 Juil 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 le 29 Juil 2019
Bingo! See answer. Let me know how that turns out.

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 29 Juil 2019
Modifié(e) : Adam Danz le 29 Juil 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 commentaires
Adam Danz
Adam Danz le 1 Août 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 le 1 Août 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 :)

Connectez-vous pour commenter.

Plus de réponses (1)

Reeny
Reeny le 1 Août 2019
Modifié(e) : madhan ravi le 1 Août 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]);

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by