How to add cells in a uitable?

5 次查看(过去 30 天)
Sai
Sai 2015-4-17
I want to add cells in the third column of my uitable. The values displayed in the uitable are coded as such. This is for Beef Teriyaki and all the other items are coded in similar fashion.
% code
qty2 = get(handles.edit2,'String');
qty2n = str2num(qty2);
beefprice = 7.95*qty2n;
DAT1 = num2cell(beefprice);
br=get(handles.uitable1,'Data');
br(2,3)=DAT1
br(2,1)={'Beef Teriyaki'}
br(2,2)=num2cell(qty2n);
set(handles.uitable1,'Data',br);
How would I add the third column 'Price' to get the total price and display it in the last row? I have tried this code.
% code
tot=get(handles.uitable1,'Data');
totn=sum(tot{:,3});
tot(8,3)=totn;
set(handles.uitable1,'Data',tot);
The says error using sum. ' Too many input arguments'
Thanks in advance!

回答(1 个)

Cindy Solomon
Cindy Solomon 2015-4-21
编辑:Cindy Solomon 2015-4-21
Hi Sai,
Since your table is a cell array, you would first need to convert your "tot" to an array to execute "sum" on it. For example, instead of doing:
sum(tot{:,3})
it is easiest to do this in 2 steps. Specifically:
totArray = [tot{:,3}]
totN = sum(totArray)
This is because "sum" does not take comma separated lists as an input, which is what tot{:.3} would return. Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by