matlab uitable: split columns into several rows

8 views (last 30 days)
SA-W
SA-W on 11 Sep 2023
Commented: SA-W on 13 Sep 2023
I construct an uitable from a struct like:
% params is a struct
T = struct2table(params);
fields = fieldnames(params)';
fig = uifigure("Name", "Parameter Editor");
paramTable = uitable(fig);
paramTable.Data = T;
paramTable.ColumnName = fields;
Say the struct has a large number of columns, then they will be all next to each other in the uitable.
What I want is to have say maximum 10 columns next to each other to see all of them at first glance,
then the next 10 in a new row,
...
How can I do this?

Answers (1)

Shubham
Shubham on 12 Sep 2023
Hi SA-W,
I understand you want to handle the large number of columns in such a way that at max 10 columns can be present in one row and next 10 can come to new row and so on.
To display the struct data in the uitable with a maximum of 10 columns per row, you can modify the code as follows:
% params is a struct
T = struct2table(params);
fields = fieldnames(params)';
% Create the uifigure and uitable
fig = uifigure('Name', 'Parameter Editor');
paramTable = uitable(fig);
% Set the Data and ColumnName properties of the uitable
paramTable.Data = T;
paramTable.ColumnName = fields;
% Calculate the number of rows and columns needed
numCols = size(T, 2);
numRows = ceil(numCols / 10);
% Set the Layout property of the uitable to 'grid' and specify the number of rows and columns
paramTable.Layout.Column = 10;
paramTable.Layout.Row = numRows;
% Set the ColumnWidth property of the uitable to a fixed value to display all columns uniformly
paramTable.ColumnWidth = repmat({100}, 1, numCols);
In this code, we calculate the number of rows and columns needed based on the number of columns in the struct. Then, we set the Layout.Column and Layout.Row properties of the uitable to specify the desired layout. Finally, we set the ColumnWidth property to a fixed value to display all columns uniformly.
Adjust the ColumnWidth value as needed to fit your data appropriately.
  1 Comment
SA-W
SA-W on 13 Sep 2023
Thank you!
A short follow-up: Is there also a way to sort the columns/rows of the uitable by types? That is, grouping strings, floats, logical,...?

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!