matlab uitable: split columns into several rows

11 Ansichten (letzte 30 Tage)
SA-W
SA-W am 11 Sep. 2023
Kommentiert: SA-W am 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?

Antworten (1)

Shubham
Shubham am 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 Kommentar
SA-W
SA-W am 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,...?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by