Uitable over whole window width

回眸只為那壹抹淺笑 提交于 2019-12-08 08:56:13

问题


I would like to programmatically (or in GUIDE) fix the matlab-uitable to be the whole width of the panel on initialisation. How can I do this?

All I've managed is to change the size in pixels of single columns. I would like the uitable to be 100% the width of the page at that time. When my window is in minimized form the GUI looks fine, but when I maximize it, the uitable is only about half the width of the page. (The uitable's height is not that of the whole panel though, around 1/2 of the whole page)

The GUI is written in matlab-guide.

This is the code that I am trying right now:

data = populateTable;
parentPosition = get(handles.uipanel4, 'position');
uitablePosition = [x y parentPosition(3)-2 parentPosition(4)-2]; 
set(handles.uitable, 'Position', uitablePosition);
set(handles.uitable, 'Visible', 'on'); 
set(handles.uitable, 'Data',data, 'ColumnFormat',{'numeric'});

回答1:


A possible solution could be to set both the position of the figure and of the table to normalized, then to set the position of the table to [0 0 1 1]

f = figure
set(f,'unit','normalized')
set(f,'position',[0.1 0.1 0.5 0.5])
data = rand(13);
for i=1:length(data)
   col_names{i}=['Col. # ' int2str(i)];
end
for i=1:length(data)
   row_names{i}=['Row. # ' int2str(i)];
end
t = uitable(f, 'Data', data,'unit','normalized', ...
   'Position', [0 0 1 1],'columnname',col_names, ...
   'rowname',row_names);



回答2:


You need to set the uitable's position property when you initialize it.

First, get the position of the uitable's parent object. That could be a uipanel or a figure. Assume hParent is a handle to the uitable's parent.

parentPosition = get(hParent, 'position');

Then, create your position vector for the new uitable, and use the parent's width and height. You might want to subtract a few pixels for padding. I'll subtract two pixels here.

uitablePosition = [x y parentPosition(3)-2 parentPosition (4)-2];

You can probably set x and y to 1 or 2 as well, depending on where the table is positioned.

uitable( ... 'units','pixels','position', uitablePosition )

You can implement the same idea in your figure's resize function.



来源:https://stackoverflow.com/questions/29980988/uitable-over-whole-window-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!