You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
background of editable cells UITABLE
22 views (last 30 days)
Show older comments
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
on 29 Jul 2019
I finally found where my problem is.
My code is :
colergen = @(color, text) ['<html><table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR></table></html>'];
data = getappdata(handles.uitable, 'data')
data{1,1} = colergen('#33ffff',num2str(data{1,1}))
set(handles.uitable, 'data', data)
I have the error :
Index exceeds the number of array elements (0).
I think the problem is at line 3, but I don't know how I can to solve it.
Have you an idea?
Reeny
on 29 Jul 2019
My exact code, in the CellSelectionCallback function, is:
row = eventdata.Indices(1)
column = eventdata.Indices(2)
setappdata(handles.uitable1, 'row', row);
setappdata(handles.uitable1, 'column', column);
if column == 1
colergen = @(color, text) ['<html><table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR></table></html>'];
data = getappdata(handles.uitable1, 'data')
data{1,1} = colergen('#33ffff',(data{1,1}))
set(handles.uitable1, 'data', data);
end
When I comment on the lines before if, I havn't the error but I can't edit my case.
Adam Danz
on 29 Jul 2019
Edited: Adam Danz
on 29 Jul 2019
Have you verified that 'data' isn't empty? One way of doing that is to put a break at the 3rd line within the conditional (at data{1,1}...) and to see the value of 'data' when the code gets to the break.
Another way of doing that is by printing out the value of 'data' just before that line.
if column == 1
colergen = @(color, text) ['<html><table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR></table></html>'];
data = getappdata(handles.uitable1, 'data')
data %will print to command window
data{1,1} = colergen('#33ffff',(data{1,1}))
set(handles.uitable1, 'data', data);
end
If your error message is "Index exceeds the number of array elements (0)." and that error is comming from the line data{1,1} = colergen..., then either "data" is empty or something's going wrong in your colergen() function.
Reeny
on 29 Jul 2019
'data' is not empty, I verified with your second method :
data =
4×2 cell array
'1' [0]
'2' [0]
'3' [0]
'4' [0]
I tried my code with another version of Matlab and I have this error :
Index exceeds matrix dimensions.
Error in Table>uitable1_CellSelectionCallback (line 131)
row = eventdata.Indices(1)
This error appears when the line set(handles.uitable1, 'data', data) is executed (I verified with break point)
Adam Danz
on 29 Jul 2019
Something's not lining up, could you show the full copy-pasted error message?
From the partial error message you shared, it looks like the error is happening on row=eventdata.... but that line is before the set(handles...) line so that doesn't make sense.
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
Accepted Answer
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
on 30 Jul 2019
This will be difficult to trouble shoot without being able to working with the code that produces the problem. Could you make a small block of code that I can run independently that produces the problem? If you can, also let me know the version of matlab you're running.
Reeny
on 30 Jul 2019
Edited: Reeny
on 30 Jul 2019
I created my uitable with GUIDE, with the parameters you can see in the following picture. 

My code is the following :
function Table_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Table (see VARARGIN)
% Choose default command line output for Table
handles.output = hObject;
data = {'1', false; '2', false; '3', false; '4', false};
setappdata(handles.uitable1, 'data', data);
set(handles.uitable1, 'data', data);
% Update handles structure
guidata(hObject, handles);
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
if isempty(eventdata.Indices)
return
end
row = eventdata.Indices(1)
column = eventdata.Indices(2)
setappdata(handles.uitable1, 'row', row);
setappdata(handles.uitable1, 'column', column);
if column == 1
colergen = @(color, text) ['<html><table border=0 width=400 bgcolor=',color,'><TR><TD>',text,'</TD></TR></table></html>'];
data = getappdata(handles.uitable1, 'data')
data{1,1} = colergen('#33ffff',(data{1,1}))
set(handles.uitable1, 'data', data);
end
My version of Matlab is R2018b.
Adam Danz
on 30 Jul 2019
I don't know what Table_OpeningFcn() is or what how it plays a role in your GUI.
When I created a quick, fake GUI with a UI table and ran your uitable1_CellSelectionCallback() function, I also could not edit the cells. However, when I set the ColumnEditable values to true, I could edit them.
handles.uitable1.ColumnEditable = [true,true]; %one for each column
Reeny
on 31 Jul 2019
Table_OpeningFcn() is a function automatically created by GUIDE when I create my UI table. The elements in this function are displayed when the GUI is opened.
Your solution does not work for me... :(
Where did you define 'data' if you don't use Table_OpeningFcn ?
Adam Danz
on 31 Jul 2019
You m file contains the following Cell Selection function
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
However, when I open you GUI in GUIDE and look at the table properties, I see that you've assigned a different Cell Selection function
@(hObject,eventdata)Test('uitable1_CellSelectionCallback',hObject,eventdata,guidata(hObject))
And you've also assigned at Cell Edit function that doesn't appear in the m file
@(hObject,eventdata)Test('uitable1_CellEditCallback',hObject,eventdata,guidata(hObject))
If "Test()" is the correct function, then those should be included in the m file.
Adam Danz
on 1 Aug 2019
Ok, this version of your code makes more sense.
Good news. I have a final answer on the issue.
Bad news. You can't have your cake and eat it too. The HTML code interferes with the ColumnEditable property within the CellSelection callback function. The explanation is a bit long so I'll break it down into parts.
- Just a side-note. When you replace the simple string with HTML code, the entire code becomes the text for that UITable cell (see code [1] below). If another section in your GUI reads the UITable, it will not read "2" in cell (2,1). It will read the long html string instead. This is a common problem.
- The CellSelection callback function is invoked every time you select a cell before you have a chance to change anything. If you move your code from the CellSelection callback function into a CellEditCallback function (first you'll need to add one from GUIDE), then you'll have the chance to edit the cell before the html comes into effect. Pro: Now you can edit the cell. Con1: the cell won't be highlighted until after you're done editing. Con2: When you select the cell to make a change, the simple string you see (ie, "2") will become the full html string shown in code [1] below. Con3: See point #1 above.
- If you decide to try out point #2 above, you'll need to change a line of your code. Currently you're not reading the current UI table. You're reading whatever was stored in "setappdata" previously. I'm not sure why you're using this approach when the first input to any callback function is the handle to the object you're manipulating. To fix that, make the change shown in code [2] below.
% [1] After selecting cell (2,1)
data =
4×2 cell array
{'1' } {[0]}
{'<html><table border=0 width=400 bgcolor=#33ffff><TR><TD>2<…'} {[0]}
{'3' } {[0]}
{'4' } {[0]}
% [2] correctly grab the current table data
data = getappdata(handles.uitable1, 'data') % INCORRECT
data = hObject.Data; % CORRECT
Recommendations
I recommend you abandon this approach alltogether. Mixing HTML with important text might look pretty but it's a nightmare to work with. If you need to highlight the most recently edited row, you could a 3rd column to your UItable that is not editable and contains no text. The purpose of this column would only be to implement the HTML colors on the row most recently edited (or whatever other rule you'd like to use).
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!
More Answers (1)
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.
See an explanation here: https://fr.mathworks.com/matlabcentral/answers/25038-how-to-change-each-cell-color-in-a-uitable#answer_261164
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]);
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)