UITable with both char and numeric data

조회 수: 12 (최근 30일)
Mike D.
Mike D. 2020년 10월 5일
답변: Mike D. 2020년 10월 5일
I know you can have a UITable with all cell array data, and you can have one with all numeric data, but I want a mix of both in the same UITable, and I also want to allow the user to Sort the column data in an App Design GUI. If I create all cell array data, having my numeric data represented as cell arrays, and I click the Sort feature at the top of the column, it will not properly sort the data numerically. For example, if my numeric column is 4e5, 8e6, 1e7, it will sort as 1e7, 4e5, 8e6, whereas I want it sorted as 4e5, 8e6, 1e7. How do I create a UITable with mixture.
This site shows you can customize the format of a UITable with all numeric: https://www.mathworks.com/help/matlab/ref/format.html#btiwmh5-1-style
But it seems, I cannot have a mixture like this:
app.UITable.ColumnFormat = {'char','shortE','char','shortE','char'};
Maybe a future version of Matlab could allow this.
Otherwise a work-around is to create a function that sorts the numeric columns: UITableDisplayDataChanged(app, event), by converting the cell array data to numeric, then sorting, then converting numeric back to cell to display in the table.

채택된 답변

Mike D.
Mike D. 2020년 10월 5일
I think I figured it out. The Data property must be Cell array which can be a mixture of data types:
data = {'text1', 4e6, 'file1', 5e-6, 'file30'; 'text33', 7e3, 'file345', 1e-4, 'file 13'; 'text888', 1e8, 'file 345', 9e-8, 'file 1333'};
app.UITable.Data = data;
app.UITable.ColumnFormat = {'char','shortE','char','shortE','char'};
Creating this data variable in a for-loop can be done like this:
for j = 1 : numel(categ)
data(j,:) = {categ{j}, temperature(j), fileNameT{j}, pressure(j), fileNameP{j}};
end
The values in the cell array must be numeric, logical or char. They can't be cell array or categorical.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by