How to change size of uitable?

133 views (last 30 days)
MC3105
MC3105 on 3 Oct 2014
Commented: Mubassira Syed on 4 Jun 2024
Hello everyone,
I have calculated a few values that I want to display in a table. Since I couldn't find any other tables for matlab I am now using uitable. The problem is that I have about 8 columns and only 2 rows. When the table is displayed it only shows me the first 3 columns and then I can scroll right to see my other values. Is there a way to make the displayed part of the table larger? I would like to see ALL my values right away!
Thanks alot!!

Accepted Answer

Orion
Orion on 6 Oct 2014
Hi,
you can resize the columns of your uitable.
once you have filled the table with your data, you can use the ColumnWidth parameter
something like :
figure;
data = rand(2,8);
u = uitable('Position',[20 20 500 70],'data',data)
pause(2)
set(u,'ColumnWidth',{50})
you can mix the use of the position and ColumnWidth parameters to get the result you prefer.
  1 Comment
Adam
Adam on 6 Oct 2014
uitable certainly isn't friendly to parameterise, especially if you have row headers which are not resizable without hacking around in the underlying java, but if you just have columns and rows with no row headers then this works. You have to mess about a little if you want the whole table to be displayed without scrollbars and without lots of extra space around it, but following Orion's advice will work if you do the maths on column width * number of columns and then add a couple of pixels (to ensure no scrollbars).

Sign in to comment.

More Answers (1)

Warren Hamilton
Warren Hamilton on 23 Mar 2017
Edited: Warren Hamilton on 23 Mar 2017
This may be a couple of years old now, but I was messing around with this earlier today. Another option is to resize the actual figure window size/position to neatly contain the uitable.
It's a little bit of a hack in the sense that this code uses hardcoded constants to allow me to set to 'outerposition' with enough space for the window boundaries etc, but it does work. There's probably a more sophisticated property to set, but I got this code working and stopped searching...
It works by
1) get the actual size of the table and set the size of the uitable object to be that size.
2) get the figure size, and modify so that it is big enough to contain the uitable data.
h = figure;
data = rand(2,8);
u = uitable('Position',[20 20 500 70],'data',data);
table_extent = get(u,'Extent');
set(u,'Position',[1 1 table_extent(3) table_extent(4)])
figure_size = get(h,'outerposition');
desired_fig_size = [figure_size(1) figure_size(2) table_extent(3)+15 table_extent(4)+65];
set(h,'outerposition', desired_fig_size);
I was doing this becauses I wanted to print the table to png so that I had something quick/dirty to insert into a draft document without mucking about with exporting to excel and importing that etc etc.. For that purpose, printing to file works, but only really if you make sure that you are printing in high resolution - using the default 'saveas' means that that quality is unusable. (again, there's ways around this using saveas, but this one works for me.)
print('-dpng', filename, '-r0'); %-r0 prints at screen resolution
  1 Comment
Mubassira Syed
Mubassira Syed on 4 Jun 2024
Dear Warren Hamilton, thank you for this little hack. I had been looking around for similar thing. It helped me as well to save the table in figure format.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!