1

I would like to programmatically (or in GUIDE) fix the 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 .

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'});
Jurgen Cuschieri
  • 658
  • 14
  • 33

2 Answers2

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);
il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • 2
    You only need to set the `uitable` unit to `normalized` (and then the position to `[0 0 1 1]` indeed). No need to change the figure unit settings. Also note that setting the `unit` setting of any uicontrol to `normalized` is the (only) way to have them automatically resize with the figure, any other `unit` setting, and you have to handle the resize in the `ResizeFcn`. – Hoki May 01 '15 at 14:56
  • guys thanks for your help. I have this in the initialisation function, set(handles.uitable, 'Data',data, 'ColumnFormat',{'numeric'}); Im getting the data to populate the table and storing it into 'data'. How do I have to change it? I tried to do it but the width stayed the same, the height just came almost the whole height of the window. – Jurgen Cuschieri May 02 '15 at 10:40
0

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.

siliconwafer
  • 732
  • 4
  • 9
  • I didn't understand how to actually set the uitablePosition. my uitable was done in Guide. right now Im doing something like this: parentPosition = get(handles.uipanel4, 'position'); uitablePosition = [x y parentPosition(3)-2 parentPosition (4)-2]; set(handles.uitable, 'Visible', 'on'); set(handles.uitable, 'Data',data, 'ColumnFormat',{'numeric'}); In the last 2 lines I have to fit in the position created in the previous 2 lines (suggested by you). any idea? – Jurgen Cuschieri May 02 '15 at 17:46
  • set(handles.uitable, 'position', uitablePosition); – siliconwafer May 04 '15 at 12:50
  • On the new suggested line I am getting this error: Error using set Value must be a 4 element vector – Jurgen Cuschieri May 04 '15 at 16:05
  • There's an extra space in one of the above lines. parentPosition(4)-2 rather than parentPosition (4)-2 – siliconwafer May 04 '15 at 16:28
  • the parentposition(3) and (4) are 1 and 1.000 respectively. therefore parentposition(3)-2 and parentposition(4)-2 are both giving a value of -1, and they must be > 0. when i remove the - the uitable doesn't even show. what could I possible be doing wrong? - see edit in original post to see my code right now – Jurgen Cuschieri May 04 '15 at 18:14
  • The units of the uipanel may not be 'pixels', so you are subtracting numbers with different units. What units is the uipanel in? – siliconwafer May 04 '15 at 19:22
  • it was normalized and I fixed it to pixels. in pixels I think I cannot even maximize.. Now it ran but this is what I got.. this when minimized: http://imgur.com/wODx9Xv (which is practically the whole window, when I want it to be half the height of the panel and the whole width of the panel) and this when maximized: http://imgur.com/P0qyGXX ... This is how it looks in minimized form without setting the position as you suggested: http://imgur.com/QvITIfV Reminder: I want the uitable to always be half the height of the panel, and the whole width & I want to be able to maximize if possible! – Jurgen Cuschieri May 04 '15 at 19:48