Creating uitable with multiple data types

7 次查看(过去 30 天)
I have a table that I need to open as a uitable. The original table has both character arrays and numeric variables. I get the following error when creating the uitable, .
Cannot concatenate the table variables _____ and ____, because their types are double and cell.
This isn't my exact code, but illustrates my problem...
%Build Table
SectionID = {'A';'B';'C';'D'};
Description = {'First';'Second';'Third';'Fourth'};
Measurement1 = [5.2;5.0;5.0;5.8];
Measurement2 = [10;11;15;10];
T = table(SectionID,Description,Measurement1,Measurement2);
%Set up figure
handles.fig = figure('Position',[400,400,500,190]);
handles.T = T;
%UI Table - TEXT ONLY - WORKS
uitable(handles.fig,'Data',handles.T{:,[1,2]},'ColumnWidth',{100},...
'ColumnName',handles.T.Properties.VariableNames([1,2]),...
'Position',[20,20,450,150]);
%UI Table - NUMERIC ONLY - WORKS
uitable(handles.fig,'Data',handles.T{:,[3,4]},'ColumnWidth',{100},...
'ColumnName',handles.T.Properties.VariableNames([3,4]),...
'Position',[20,20,450,150]);
%UI Table - TEXT AND NUMERIC - DOES NOT WORK
uitable(handles.fig,'Data',handles.T{:,:},'ColumnWidth',{100},...
'ColumnName',handles.T.Properties.VariableNames(:),...
'Position',[20,20,450,150]);

采纳的回答

dpb
dpb 2016-11-16
编辑:Walter Roberson 2016-11-16
Indeed, the error message is right--"you can't do that!" of trying to concatenate cell array and non-cell array values.
You'll have to convert the table into a cell array to pass to uitable; it isn't table-aware so can't use the table directly, either.
I don't have release that includes the table class, but I'd think table2cell(T) would be just the ticket here...see table2cell()
  3 个评论
Bryan Wilson
Bryan Wilson 2016-11-17
What confuses me is I don't know why it's trying to concatenate at all. For the record...here's the code that DOES work.
%Build Table
SectionID = {'A';'B';'C';'D'};
Description = {'First';'Second';'Third';'Fourth'};
Measurement1 = [5.2;5.0;5.0;5.8];
Measurement2 = [10;11;15;10];
T = table(SectionID,Description,Measurement1,Measurement2);
%Set up figure and UI Table
handles.fig = figure('Position',[400,400,500,190]);
handles.T = table2cell(T);
uitable(handles.fig,'Data',handles.T(:,:),'ColumnWidth',{100},...
'ColumnName',T.Properties.VariableNames(:),...
'Position',[20,20,450,150]);
Walter Roberson
Walter Roberson 2016-11-17
For a table, the notation handles.T{:,:} is equivalent to horzcat() of all of the T{:,1}, T{:.2}, ... and for that to work they need to be compatible data types. T{:,:} notation is for extracting to array, not for extracting to cell.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by