increase size of uitable dynamically
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (4)
Andrew Newell
on 19 Mar 2011
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.
0 Comments
sotiris
on 19 Mar 2011
2 Comments
Oleg Komarov
on 19 Mar 2011
Can you specify the dynamics? What kind of event should trigger the dynamic addition of rows?
Andrew Newell
on 24 Mar 2011
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 Comments
Vishwarath Taduru
on 25 Nov 2015
Edited: Vishwarath Taduru
on 25 Nov 2015
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
on 25 Nov 2015
Vishwarath, the easiest way would be to delete the final "end" of the code shown here.
Bilen Oytun Peksel
on 19 Nov 2012
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 Comment
Jan
on 19 Nov 2012
@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.
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!