Help me please to correct this code

Hello, I met some difficulties using uitable in GUI especially that this table must extract data from a database. Please how can I modify this code so that the number of rows in the table is dynamic (the number of rows increments based on the number of data in the database)? what should the variables dat and columnformat contain?? knowing that I use matlab 2010. thanks
[i m a] = mym('SELECT * FROM image');
num=mym('SELECT COUNT(*) FROM image');
dat = {??????????};
columnname = {'id', 'Mean', 'Average'};
columnformat = {???????};
t = uitable('Units','normalized','Position',...
[0.1 0.1 0.9 0.9], 'Data', dat,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'RowName',[]);

Réponses (1)

Walter Roberson
Walter Roberson le 24 Sep 2012

0 votes

Data should be a cell array containing the data to be put into the table. The number of rows and columns are determined by the size of the data provided and not by columnname or the other parameters.
columnformat would depend on the datatype of the data and upon how you want it to look. Often you do not need to provide columnformat as uitable makes reasonable defaults depending on the Data you provide. If you do need to provide it, then columnformat should be a cell array of strings.

4 commentaires

Sorry but I couldn't understand how can i use a cell array. I tried this code knowing that the table in the database and the table in GUI contain 3 columns
[i m a] = mym('SELECT * FROM image');
num=mym('SELECT COUNT(*) FROM image')
for i=1:num
dat{i} = {i, m, a}
end
columnname = {'id', 'Mean', 'Average'};
columnformat = {'char', 'numeric', 'numeric'};
columneditable = [false false false];
t = uitable('Units','normalized','Position',...
[0.1 0.1 0.9 0.9], 'Data', dat,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable,...
'RowName',[]);
I had this error
dat =
{1x3 cell}
dat =
{1x3 cell} {1x3 cell}
??? Error using ==> uitable
Values within a cell array must be numeric, logical, or char
Error in ==> uitable at 56
thandle = builtin('uitable', varargin{:});
Error in ==> diagnostics>diagnostics_OutputFcn at 90
t = uitable('Units','normalized','Position',...
Error in ==> gui_mainfcn at 265
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in ==> result at 42
gui_mainfcn(gui_State, varargin{:});
Walter Roberson
Walter Roberson le 24 Sep 2012
Modifié(e) : Walter Roberson le 24 Sep 2012
for i=1:num
dat(i,:) = {i, m, a}
end
Carole
Carole le 24 Sep 2012
Modifié(e) : Carole le 24 Sep 2012
thanks but i have the same error
dat =
{2x1 cell} [2x1 double] [2x1 double]
dat =
{2x1 cell} [2x1 double] [2x1 double]
{2x1 cell} [2x1 double] [2x1 double]
??? Error using ==> uitable
Values within a cell array must be numeric, logical, or char
Error in ==> uitable at 56
thandle = builtin('uitable', varargin{:});
Error in ==> diagnostics>diagnostics_OutputFcn at 90
t = uitable('Units','normalized','Position',...
Error in ==> gui_mainfcn at 265
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in ==> result at 42
gui_mainfcn(gui_State, varargin{:});
One implication is that each of your "i" is a cell array. What is in that cell array, and how do you want to represent it in the uitable ?
Meanwhile, try this: after the "for" loop that creates "dat", use
dat = [ vertcat(dat{:,1}), num2cell(cell2mat(dat(:,2:end))) ];
The first part of this might fail, as it depends upon what is inside the cells.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Question posée :

le 24 Sep 2012

Community Treasure Hunt

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

Start Hunting!

Translated by