Creating uitable with multiple data types

18 Ansichten (letzte 30 Tage)
Bryan Wilson
Bryan Wilson am 16 Nov. 2016
Kommentiert: Walter Roberson am 17 Nov. 2016
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]);

Akzeptierte Antwort

dpb
dpb am 16 Nov. 2016
Bearbeitet: Walter Roberson am 16 Nov. 2016
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 Kommentare
Bryan Wilson
Bryan Wilson am 17 Nov. 2016
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 am 17 Nov. 2016
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by