How to update table with new data with a GUI

I am working on a project and I am using an interactive GUI that generates new user IDs. I already have the base GUI where the user can input their information but I would like to know how to add their inputted data into a pre-existing table.

 Accepted Answer

% create (pre-existing) table:
t = uitable( ...
'ColumnFormat',{'char' 'char' 'numeric' {'Coke' 'Pepsi' 'Dr. Pepper'} 'logical'}, ...
'ColumnName',{'Last Name','First Name','Age','Drink','Smoke'}, ...
'ColumnEditable',true(1,5), ...
'ColumnWidth',{78 78 45 100 40}, ...
'Data',{'Smith' 'Al' 24 'Coke' true; 'Jones' 'Betty' 33 'Dr. Pepper' false}, ...
'Position',[10 10 420 420]);
copyobj(t,figure()); % just for demo, to get two figures to show up here
% make some new data to add to the table:
new_data = {'Carlin' 'James' 51 'Pepsi' false};
% add the new data to the end of the existing table:
set(t,'Data',[get(t,'Data'); new_data]);

2 Comments

How would you add the data in if the user puts it into the given text boxes? Since the example you used was you manually putting in the data. Thank you for the help btw!
% say you have edit boxes called handles.lastName,
% handles.firstName, handles.age, handles.drink, handles.smoke
% (this is for uicontrols, i.e., what would be used in GUIDE/figure())
new_data = { ...
get(handles.lastName,'String') ...
get(handles.firstName,'String') ...
str2double(get(handles.age,'String')) ... % convert age String to a number
get(handles.drink,'String') ...
get(handles.smoke,'String') ...
};
% (or without get()):
new_data = { ...
handles.lastName.String ...
handles.firstName.String ...
str2double(handles.age.String) ... % convert age String to a number
handles.drink.String ...
handles.smoke.String ...
};
% if you have an AppDesigner/uifigure() GUI then the concept is the same
% but the syntax is different, and it depends on whether the each edit box
% is a NumericEditField or an EditField (and here they might be fields in
% "app" rather than "handles").
% so, say the one for "age" is a NumericEditField and the others are EditFields
new_data = { ...
get(app.lastName,'Value') ... % Value of an EditField is a character vector
get(app.firstName,'Value') ...
get(app.age,'Value') ... % Value of a NumericEditField is a number
get(app.drink,'Value') ...
get(app.smoke,'Value') ...
};
% (or without get()):
new_data = { ...
app.lastName.Value ... % Value of an EditField is a character vector
app.firstName.Value ...
app.age.Value ... % Value of a NumericEditField is a number
app.drink.Value ...
app.smoke.Value ...
};
% then, in any case, update the table data the same way as before:
% add the new data to the end of the existing table:
set(t,'Data',[get(t,'Data'); new_data]);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 8 Apr 2022

Edited:

on 8 Apr 2022

Community Treasure Hunt

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

Start Hunting!