MATLAB: Help: Table cannot edit in a programmatic GUI

table cannot edit in a programmatic gui

Hi everybody,
Could someone tell me what's wrong with my code? I created a figure with a table in GUIDE and converted it to programmatic by Fig2m (by Thomas Montagnon). Then when I run the figure, the table was shown but cannot edit although I set it editable in GUIDE.
The command window showed 'Warning: Table data is not editable at this location.'
The code:
% --- FIGURE -------------------------------------
handles.figure1 = figure( ...
'Tag', 'figure1', ...
'Units', 'characters', ...
'Position', [102.8 24.2307692307692 114 33], ...
'Name', 'untitled1', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- UITABLE -------------------------------------
handles.uitable1 = uitable( ...
'Parent', handles.figure1, ...
'Tag', 'uitable1', ...
'UserData', zeros(1,0), ...
'Units', 'characters', ...
'Position', [12.2 8 36.8 21], ...
'BackgroundColor', [1 1 1;0.961 0.961 0.961], ...
'ColumnEditable', [true,true], ...
'ColumnFormat', {'char' 'char' }, ...
'ColumnName', {'1','2'}, ...
'ColumnWidth', {'auto','auto'}, ...
'RowName', {'1','2','3','4'});
Thank you.

Best Answer

Hi,
You need to initialize the type the Data parameter. by default, Matlab consider it is a double. But you want to put strings in your table. So just specify an cell with empty string when creating the uitable.
% --- FIGURE -------------------------------------
handles.figure1 = figure( ...
'Tag', 'figure1', ...
'Units', 'characters', ...
'Position', [102.8 24.2307692307692 114 33], ...
'Name', 'untitled1', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- UITABLE -------------------------------------
% Initialize empty string for components of the Data
Data=cell(4,2);
for i = 1:numel(Data)
Data{i} = '';
end
handles.uitable1 = uitable( ...
'Parent', handles.figure1, ...
'Tag', 'uitable1', ...
'UserData', zeros(1,0), ...
'Units', 'characters', ...
'Position', [12.2 8 36.8 21], ...
'BackgroundColor', [1 1 1;0.961 0.961 0.961], ...
'ColumnEditable', [true,true], ...
'ColumnFormat', {'char','char' }, ...
'ColumnName', {'1','2'}, ...
'ColumnWidth', {'auto','auto'}, ...
'RowName', {'1','2','3','4'},...
'Data',Data); % add the "string" Data