Adding a new mixed type row to a GUIDE uitable
Show older comments
I have created a GUI to display the contents of a data structure (loaded from an xml file) in an uitable of fixed width (8 columns of information). The user can simply edit the existing data by typing in the required box and any changes are then assigned back to the variable. To make it more useful I also added a button that allows the user to add a new row to the table (for cases where the xml is originally empty or when more data needs to be added).
My uitable callback id defined as:
% --- Executes when entered data in editable cell(s) in uitable2.
function uitable2_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable2 (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
ParticleTypeInfo = get(hObject, 'data');
assignin('base','ParticleTypeInfo',ParticleTypeInfo)
For the button to add an extra row the call back is:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data = get(handles.uitable2,'Data');
data(end+1,:) = {''};
set(handles.uitable2,'Data',data)
This code works fine except for one problem - my columns are of mixed type. Columns 1,2 and 8 are strings while the rest are numeric. So when I add a new row, all my columns have strings.
So I tried changing to this for the button callback:
data(end+1,:) = [];
which makes everything a number. Typing a string into columns 1,2 or 8 then keeps returning NAN.
How do I fix this to add a new row of mixed types? I've already defined the column data types as text and numeric in the table editor, but I guess that only applies to the data before I add any new rows.
Could I use columnformat for any new rows added?
'columnformat',{'numeric','char','logical'},
Accepted Answer
More Answers (0)
Categories
Find more on App Building 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!