How can i use CellSelectionCallback correctly?

조회 수: 5 (최근 30일)
Alvindra Pratama
Alvindra Pratama 2016년 5월 7일
댓글: Alvindra Pratama 2016년 5월 11일
I had a problem when I memlilih one cell in the table to be displayed into the text edit, when I choose a cell 1 then that arises is ID = 1, Name = f, Address = f (performed in accordance with what I have chosen) But when I select cell 2, appearing instead as when I select cell 1, how do I prevent the display according to the data in cell 2?
Here is my code in function myTabel_CellSelectionCallback :
try
al= eventdata.Indices;
dataseleksi=get(handles.myTabel,'Userdata');
getdata=dataseleksi(al);
%kode=getdata{1};
[ mydata,header,no ] = Lihat()
var1=mydata(1,1);
var2=mydata(1,2);
var3=mydata(1,3);
set(handles.txtid,'string',var1);
set(handles.txtnama,'string',var2);
set(handles.txtalamat,'string',var3);
catch end
  댓글 수: 2
Jan
Jan 2016년 5월 7일
I do not understand the text of your question. What does "I had a problem when I memlilih one cell in the table to be displayed into the text edit" mean? What is "memlilih"?
Alvindra Pratama
Alvindra Pratama 2016년 5월 7일
I'm sorry for that, here the correct sentences : I had a problem when I choose one cell in the table to be displayed into the text edit, when I choose a cell 1 then that arises is ID = 1, Name = f, Address = f (performed in accordance with what I have chosen) But when I select cell 2, appearing instead as when I select cell 1, how do I prevent the display according to the data in cell 2?
I hope you can help me, please

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 5월 8일
Alvindra - why are you using the property UserData when trying to access data in your table?
dataseleksi=get(handles.myTabel,'Userdata');
Unless something is different in your version of MATLAB, the data in the table is stored in the Data property, so the above would be replaced with
dataseleksi=get(handles.myTabel,'Data');
Then, to access a specific element/cell within that table, you can use the row and column indices from
al = eventdata.Indices;
as
cellIdcs = eventdata.Indices;
dataseleksi = get(handles.myTabel,'Data');
cellData = dataseleksi(cellIdcs(1), cellIdcs(2))
Try the above and see what happens!
  댓글 수: 5
Geoff Hayes
Geoff Hayes 2016년 5월 10일
Alvindra - the first three lines of your myTabel_CellSelectionCallback callback do something similar to what we have discussed above
cellIdcs = eventdata.Indices;
dataseleksi = get(handles.myTabel,'Data');
cellData = dataseleksi(cellIdcs(1), cellIdcs(2))
So cellData corresponds to the element in the selected cell. But then you don't do anything with this variable and instead query the database with
[ mydata,header,no ] = Lihat();
You then take the first row of myData and do
var1=mydata(1,1);
var2=mydata(1,2);
var3=mydata(1,3);
set(handles.txtid,'string',var1);
set(handles.txtnama,'string',var2);
set(handles.txtalamat,'string',var3);
What is the purpose of re-querying the database especially as you don't include any clauses that may indicate which row to get the data from? I suppose that instead of getting the elements from the first row of mydata, you could instead grab the elements from the row which you have selected the cell. i.e.
rowIdx = cellIdcs(1);
[mydata, header, no] = Lihat();
var1 = mydata(rowIdx,1);
var2 = mydata(rowIdx,2);
% etc.
But if the kth row of the table is identical to the kth row in the database (assuming that you haven't removed or inserted any rows) then why not just take the elements from the table to populate the text controls?
rowIdx = cellIds(1);
var1 = dataseleksi{rowIdx,1);
var2 = dataseleksi{rowIdx,2);
var3 = dataseleksi{rowIdx,3);
% etc.
With the above, you avoid querying the database again.
Alvindra Pratama
Alvindra Pratama 2016년 5월 11일
Thank you very much, the answer to your very helpful to me and my problem in this case has been resolved

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by