Creating uitable with multiple data types

조회 수: 13 (최근 30일)
Bryan Wilson
Bryan Wilson 2016년 11월 16일
댓글: Walter Roberson 2016년 11월 17일
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