UITable with both char and numeric data

11 views (last 30 days)
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.

Accepted Answer

Mike D.
Mike D. on 5 Oct 2020
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.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!