# [Editable uitable] Calculate result based on user input

2 ビュー (過去 30 日間)
Filipe 2021 年 1 月 6 日
コメント済み: Filipe 2021 年 1 月 6 日
Hi there,
I have created a uitable in GUI. Consists on a nx3 table. The first two columns (A,B) are user input. The third should be the result of a computation (C=A*B). How can I manage to achieve this? This is my current code, in which I initialy input random values.
Thanks a lot,
Filipe
function MyTable
f = figure('Position',[300 300 3000 400]);
% Column names and column format
columnname = {'A','B','C'};
columnformat = {'bank','bank','bank'};
% Define the initial displayed data
d = {randi([0 10],1,1) randi([0 10],1,1) randi([0 10],1,1);}; % I guess this is my first mistake because the third column should be already the result of a computation but I don't know how to do it
% Create the uitable
hTable = uitable(f,'Position',[0 0 3000 400],'Data', d,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', [true true false],... % I set the last to false since it is not supposed to be editable.
'RowName',[]);
get(hTable,'Data')
end
##### 2 件のコメントなしを表示なしを非表示
Walter Roberson 2021 年 1 月 6 日

d = {randi([0 10],1,1) randi([0 10],1,1)};
d{3} = d{1} .* d{2};
Beyond that, you need a CellEditCallbackFcn parameter in the uitable() call to update d{3} based upont he current d{1} and d{2}
Filipe 2021 年 1 月 6 日
Thanks for your answer Walter. I was already able to solve the first part fo my problem. However, I'm quite new to GUI's and the uitable function. Could you tell me how to implement a CellEditCallbackFcn parameter in the uitable()? Maybe with a simple example? Thanks once again,
Filipe

サインインしてコメントする。

### 回答 (1 件)

Maximilian Schönau 2021 年 1 月 6 日
Right click on your table, --> Callbacks --> Add CellEditCallback
This will create a subsection in your app code. Whenever a cell is edited by the user, this function will run.
So you can use this Callback to calculate the result of the table inputs and update the table accordingly.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Filipe 2021 年 1 月 6 日
Thanks a lot. I think it's solved!

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeDiscrete Math についてさらに検索

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by