3

I am using uitable in matlab GUI. In this GUI, rows and columns of uitable after each processing and hence I can't use Position property of uitable. when I draw table some area remain blank and its position is also always in left lower cornser of GUI figure. Image is shown below:

enter image description here

I want to remove white area from table i.e table automatically resize itself according to row and columns.

How I can do this?

Community
  • 1
  • 1
Naseeb Gill
  • 661
  • 10
  • 23

1 Answers1

3

A possible way-around to handle this issue: uitable row height and column width are unrelated to the table's dimensions, since a table can contain any number of rows/cols. This is why the table has scrollbars. For this reason, resizing the table only affects the so-called scrollbars viewport, and does NOT affect any internal aspect such as row height or column width. You can trap the resizing callback and programmatically modify the row height and column width depending on the new table size. However, I suggest to NOT do this because most users are used to the current behavior, not just in Matlab but in most GUI-based applications.

Another possible way-around can be if you normalize your units, using 'FontUnits', 'Normalized', it may help you. Since Font size will change, also will your row and column width, but the moment the font does not need to expand column width, column will stop re-sizing.

The following code will serve the purpose.

clear all;
clc;

%% Create a random dataset with any number of rows and columns
data = rand(10, 15);

%% Create a uitable
t = uitable('Data',data);

%% Set the position and size of UITable
% Position(1) = Distance from the inner left edge of the figure
% Position(2) = Distance from the inner bottom edge of the figure
% Position(3) = Distance between the right and left edges of  rectangle containing the uitable
% Position(4) = Distance between the top and bottom edges of  rectangle containing the uitable
% Extent(1) = Always zero
% Extent(2) = Always zero
% Extent(3) = Width of uitable
% Extent(4) = Height of uitable
t.Position = [350 350 t.Extent(3) t.Extent(4)];

%%End
Waseem Anwar
  • 301
  • 1
  • 7
  • 2
    Hello, @Waseem Anwar, by using t = uitable('Data',rand(6,3)); t.Position(3:4) = t.Extent(3:4); we can change the size but figure always comes at left bottom corner of figure and when we draw two tables of different rows second one cover first one partially or fully. How to change location of table when drawing using above commands. What is the meaning of (3:4) in above commands? – Naseeb Gill Apr 06 '17 at 03:55
  • Hi @NaseebGill, That's great. Now you are very close to your solution. In order to understand t.Position(3:4) = t.Extent(3:4), we need to look inside both properties of uitable .i.e. position and extent. Position is a 4 element vector [(Distance from the left edge of the figure) (Distance from the bottom edge of the figure) (width of the table) (height of the table)]. It means you can change the position of the table by playing with first two values. At the other hand, Extent is a read-only (you can't set the parameters) 4 element vector which gives you width(3) and height(4) of the table. – Waseem Anwar Apr 06 '17 at 08:35
  • continued... While first two elements of Extent are always zero.so statement ( t.Position(3:4) = t.Extent(3:4);) actually gets the width and height of table from 3rd and 4th element of Extent and place these values in 3rd and 4th elements of Position. In order to set the location, you can play with first two elements of Position. The lower left corner corresponds to (0,0) while upper left corner corresponds to maximum size of figure. Please note, Position and Extent are different properties. Extent corresponds to the table of values while Position corresponds to the table + container (white). – Waseem Anwar Apr 06 '17 at 08:45
  • 1
    Nice. Didn't know that it was given in the documentation – Sardar Usama Apr 06 '17 at 09:13
  • and how to change the width and height of tab group in matlab ? I see on their website that those properties are read only ... any help ? – Ali Yar Khan Apr 15 '19 at 06:37