Modify cell value of a row based on other cell value in same row uitable

Hi,
function uitable4_CellEditCallback(hObject, eventdata, handles)
a=get(handles.uitable4,'Data');
a = cell2table(a);
a.a6 = (10 / a.a5)*1^4; %where a.a6 is the column to be auto modify after editing column a.a5
a = table2cell(a);
set(handles.uitable4, 'Data',a);
Practically, I want every time I modify the value in a cell from a.a5 column, the a.a6 to be modified automaticaly, for the first row it's working but when I add another row i get the error:
To assign to or create a variable in a table, the number of rows must match the height of the table.
Practically, I unerstand the error but how to make the code working for every row? Thanks!

11 Comments

You may try to directly update the cell on another column (noticed the following example is assuming the cells are on the same row) based on the edited value.
function cellUpdate(src,event)
% Get the row number of your edited cell
rownum = event.Indices(1);
colnum = event.Indices(2);
% Doing some operation from the modified value and update the value on
% another column, following example is column #5.
src.Data(rownum,5)=src.Data(rownum,colnum)+1000; % Add 1000 to the new value
end
Forgot to mention that you can modify the content of your callback based on the example. Sorry about that.
function uitable4_CellEditCallback(hObject, eventdata, handles)
% Get the row number of your edited cell
rownum = eventdata.Indices(1);
colnum = eventdata.Indices(2);
% Doing some operation from the modified value and update the value on
% another column, following example is column #5.
hObject.Data(rownum,5)=hObject.Data(rownum,colnum)+1000; % Add 1000 to the new value
end
rownum = event.Indices(1);
What represent (1) or (2)?
Where I should place the function and when the action is performed?
Thanks!
They represent the row and column number of your edited cell.
So you can easily extract the updated data inside the callback
Undefined operator '+' for input arguments of type 'cell'.
Error in Generator_v12>uitable4_CellEditCallback (line 1042)
hObject.Data(rownum,5)=hObject.Data(rownum,colnum)+1000; % Add 1000 to the new value
how can I convert cell
Change to Curly braces, {}
hObject.Data{rownum,5}=hObject.Data{rownum,colnum}+1000
"They represent the row and column number of your edited cell."
So if I have multiple rows, I must take the indices for all, because user might input many rows...?
The callback would update each cell individually.
When the user updated the first cell, the callback automatically update the value on another column. So the callback can do it for you for any rows.
rownum = eventdata.Indices(1);
colnum = eventdata.Indices(2);
% Doing some operation from the modified value and update the value on
% another column, following example is column #5.
hObject.Data{rownum,6}=hObject.Data{rownum,colnum}+1000; % Add 1000 to the new value
It update the column 6 value but the column 5 its blocked fot editing
My fault, numlock was off :) soryy for that!
The problem is that column 6 values is modified every time i edit any cell from that row
This is another issue, you need to make uitable editable as follows:
Assume your uitable have 6 columns and if you would like to allow user to edit column 5 only, do the following.
uitable4.ColumnEditable=[false,false,false,false,true,false]
OR only update column 6 when column 5 is being edited.
function uitable4_CellEditCallback(hObject, eventdata, handles)
% Get the row number of your edited cell
rownum = eventdata.Indices(1);
colnum = eventdata.Indices(2);
% Update column 6 when column 5 has a new value
if isequal(colnum,5)
hObject.Data(rownum,6)=hObject.Data(rownum,colnum)+1000; % Add 1000 to the new value
end
end
if colnum == 5;
hObject.Data{rownum,6}=hObject.Data{rownum,colnum}+1000; % Add 1000 to the new value
end
that resolve the problem
Thanks @Simon Chan for your patience, and answers !

Sign in to comment.

 Accepted Answer

Just move the last comment as an answer.
function uitable4_CellEditCallback(hObject, eventdata, handles)
% Get the row number of your edited cell
rownum = eventdata.Indices(1);
colnum = eventdata.Indices(2);
% Update column 6 when column 5 has a new value
if isequal(colnum,5)
hObject.Data(rownum,6)=hObject.Data(rownum,colnum)+1000; % Add 1000 to the new value
end
end
Please accept the answer if it is useful for you.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2015a

Community Treasure Hunt

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

Start Hunting!