필터 지우기
필터 지우기

How to add cells in a uitable?

조회 수: 5 (최근 30일)
Sai
Sai 2015년 4월 17일
편집: Cindy Solomon 2015년 4월 21일
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에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by