NaN elements during table data input - Matlab GUI

조회 수: 3 (최근 30일)
Peter Borda
Peter Borda 2015년 1월 7일
댓글: Adam Taher 2015년 5월 13일
I have a simple gui with a table: three columns, all set to editable, containing texts. With an additional button I can append empty rows to the end of the table. However, every time I write something in the new, empty cells at the bottom of the table and hit enter the value automatically changes to NaN. I've figured out that if I write a numerical value in the cells, there's no problem, so because of some reason matlab handles the new cell elements as if their type would be numeric. Any ideas how to solve this? I tried to add a CellEditCallback to the object, but the value update seems to happen before the callback would be evaluated...
  댓글 수: 2
Baris Gungordu
Baris Gungordu 2015년 1월 19일
Dear Peter,
I have the same problem. I want the user to define only the first column for each added row. However, I could not apply your code.
I create my table with the following code: (Counters are used to number the rows created)
counter1 = counter1 + 1;
data = get(handles.uitable1, 'Data');
data(end+1,:) = [0 counter1-counter2+1 0 0 0 0];
set(handles.uitable1,'Data',data)
The rows created are all 0's and user can edit it. I want the first 0 to be 'Name' and to be editable by the user. I appreciate if you can give me an idea.
Peter Borda
Peter Borda 2015년 1월 23일
In my case initialization of the new lines to some value - as suggested by Geoff below - solved the problem. However in your case probably this works too:
data(end+1,:) = {'Name' counter1-counter2+1 0 0 0 0};
Take care of the curly bracket, otherwise it throws back an error! If you can't edit the first column, than you probably have to set the ColumnEditable property if you haven't done that already.

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

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 1월 8일
Peter - how are you creating the new row? Is each element (of it) a cell string or an empty matrix? If I create my new row like
% get the current data
data = get(handles.uitable1,'Data');
% determine the number of columns and create the new row of empty matrices
numCols = size(data,2);
newRow = cell(1,numCols);
% add the new row to data and save it to the table
data = [data ; newRow];
set(handles.uitable1,'Data',data);
then I observe the same behaviour as you - each non-numeric entry is converted to NaN since all elements in my new row are empty matrices (i.e []).
But if I create the new row as a row of strings by assigning each element of newRow to a cell with an empty string as
newRow = cell(1,numCols);
newRow(:) = {''};
or the simpler
newRow = repmat({''},1,numCols);
then I am able to add both numeric and non-numeric data to the new row of the table since each element in the row is now a string.
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2015년 1월 9일
Glad I was able to help, Peter!
Adam Taher
Adam Taher 2015년 5월 13일
Hi, i'm trying to do the same thing, but by adding new columns this time. Adding new rows works fine, but not columns.
I used the newRow code to do it:
data = get(handles.uitable, 'data');
numRows = size(data,2); newCol = cell(numRows,1); % or newCol = repmat({''},1,numRows); data = [data ; newCol]; set(handles.uitable,'Data',data);
What it does is when i press my "Add column" push button, it keeps adding rows instead of columns.
Thanks for the help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by