increase size of uitable dynamically

13 次查看(过去 30 天)
sotiris
sotiris 2011-3-19
hello to everyone.
I just wanted to know if there is a way to increase the size of a uitable dynamically. I am making a gui and although i do know the number of columns, unfortunately i do not know the number of rows. The number of rows depends on the amount of data the user will put in, so i would like my row number to increase with every new entry in the table.
Thanks in advance Sotiris

回答(4 个)

Andrew Newell
Andrew Newell 2011-3-19
I'm not sure how you want to enter the data, but I'll sketch out one approach below: First, set up your table like this:
ht = uitable(...
'Data',tableData, ...
);
This will give your table as many rows as tableData. Then create a button for adding a row:
uimenu('Label','Add row','Callback',@addCallback);
and have a callback function to add a blank row to the top:
function addCallback(~,~)
% Add a blank row to top of table
tableData = [blankRow; tableData];
set(ht,'Data',tableData)
end
I have left out a lot of details; you'll have to consult the documentation to implement your particular problem.
EDIT: If you want to add rows without pressing a button, the easiest way would be to start with a lot of blank rows at the bottom. This would be similar to what you see in a spreadsheet. Then you could use a button for adding "pages" instead of rows.

sotiris
sotiris 2011-3-19
hello Andrew,
thank you very much for your answer, it seems like a good workaround. On the other hand though i take it there isn't a straightforward solution like change something in the properties of the uitable so that it will increase the number of its rows dynamically?
  2 个评论
Oleg Komarov
Oleg Komarov 2011-3-19
Can you specify the dynamics? What kind of event should trigger the dynamic addition of rows?
sotiris
sotiris 2011-3-24
Well, for example whenever the available cells are full i would like automatically a new row to be added.

请先登录,再进行评论。


Andrew Newell
Andrew Newell 2011-3-24
Here is a simple example of a function that will create a table with a blank row and add rows as you go. I have chosen to add a row as soon as any cell in a blank row is given a value, but you could alter that. It will need some work to be fully satisfactory, but it is proof of concept.
function testDynamicRows
% Test dynamic addition of rows
figure
emptyRow = {'',''};
tableData = emptyRow;
uitable('ColumnEditable', true(1,2), ...
'Data',tableData,'CellEditCallback',@editCallback);
function editCallback(hObject,eventData)
% hObject handle to uitable
% eventData structure with fields:
% Indices
% PreviousData
% EditData
% NewData
row = eventData.Indices(1);
col = eventData.Indices(2);
newValue = eventData.NewData;
if ~isempty(newValue)
if all(cellfun(@isempty,tableData(row,:)))
tableData = vertcat(tableData,emptyRow);
end
tableData{row,col} = newValue;
set(hObject,'Data',tableData)
end
end
end
EDIT: A problem with this approach is that when you add a row to the table, the cell selection ("focus") is lost and there is no way to set it programmatically - at least, without using some undocumented Java. Thus, you have the annoying feature that you have to click on the next cell after each edit. I think a hybrid approach is best - have a lot of blank rows at the end and only add to them if you run out.
  3 个评论
Vishwarath Taduru
Vishwarath Taduru 2015-11-25
编辑:Vishwarath Taduru 2015-11-25
How do I do this in the GUIDE environment? When I copy the code in the editor and run it returns the following error.
The function "testDynamicRows" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.
Walter Roberson
Walter Roberson 2015-11-25
Vishwarath, the easiest way would be to delete the final "end" of the code shown here.

请先登录,再进行评论。


Bilen Oytun Peksel
Bilen Oytun Peksel 2012-11-19
I have a correction to Andrew's solution:
instead of :
if all(cellfun(@isempty,tableData(row,:)))
u do:
if nnz(~cellfun(@isempty,tableData(row,1:3)))>1
that way as you enter your 3rd entry and press the key it will automatically create another row.
  1 个评论
Jan
Jan 2012-11-19
@Bilen: Andrew has posted two solutions. Please add a comment to a solution in the comment section.
cellfun('isempty') is faster than cellfun(@isempty). Although the user will not notice this for small tables, it reduces the energy consumption.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by