having trouble to insert text in GUI table

I want to make a table, 'uitable', with 4 collumns and a variable number of rows. The number of rows is determined by an edit text box, 'edit1'. The problem is that I want to insert text in the first collumn, but as soon as i insert any caracter that is not a number, the cell automatically goes to NaN. Can you help me?
edit1 = str2num(char(get(hObject,'String')));
if ~isempty(edit1) && edit1>0
edit1 = ceil(edit1);
data = cell(edit1,4);
% convert the values matrix to a cell array
valuesAsCell = mat2cell(data);
% update the table
set(handles.uitable,'Data',valuesAsCell{:});
end

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 24 de En. de 2015
Diogo - this may be a problem that is similar to http://www.mathworks.com/matlabcentral/answers/169197-nan-elements-during-table-data-input-matlab-gui. Your line of code
data = cell(edit1,4);
creates the cell array where each element is the empty array, []. If you try to populate any of these elements with strings, then you see NaN. Try instead to initialize this array to one of empty strings and reduce your code to simply
data = cell(edit1,4);
data(:) = {''};
% update the table
set(handles.uitable1,'Data',data);
I used the above code in a simple GUI, and was able to enter non-numeric data in any column (as well as numeric data which will still be considered as a string). Note that I removed the code to convert the array to a cell array (the call to mat2cell) since data is already a cell array.

2 comentarios

Diogo Queirós
Diogo Queirós el 24 de En. de 2015
aaaaaannnnnddd IT WORKS!!
thanks Geoff! sorry for my poor search of the problem
Geoff Hayes
Geoff Hayes el 24 de En. de 2015
Glad it worked out, Diogo!

Iniciar sesión para comentar.

Más respuestas (1)

Zoltán Csáti
Zoltán Csáti el 23 de En. de 2015
It is because you supplied a string. Convert it to number.
edit1 = str2num(edit1);
edit1 = ceil(edit1);
...

3 comentarios

its already in number. check the first line.
edit1 = str2num(cha* r(get(hObject,'String')));
if ~isempty(edit1) && edit1>0
...
what i meant was, when inserting data in the UI table, the first collumn must be a string, the other 3 are numbers. For example, for edit1=3:
String | | Number | | Number | | Number
ex1 | | 15 | | 2034 | | 3000
ex2 | | 35 | | 2064 | | 2056
ex3 | | 21 | | 2012 | | 2076
with my code i can´t insert the string on the first collumn, but the construction of the table and the other collumns work fine.
Zoltán Csáti
Zoltán Csáti el 23 de En. de 2015
Could you send me your program. I would look at it.
Diogo Queirós
Diogo Queirós el 24 de En. de 2015
the gui is in portuguese, in case you need to translate. thank you in advance!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de En. de 2015

Comentada:

el 24 de En. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by