Adding a new mixed type row to a GUIDE uitable

4 views (last 30 days)
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

John
John on 12 Nov 2015
Ok, after a good nights sleep I've actually answered my own question! The easiest way is to simply define prescribed data of the format you want. Then it wall be correct but necessitating change. It also forces to user to change the data to something that makes sense since the default values are very different from the expected data.
I simply changed my new line code to the following:
data(end+1,:) = {'Name1','Name2', 1e-4,1e-8,1e-10,1e-10,1e-10,'yes'};
And now all my new rows are the correct format with default values that need to be changed by the user, thus acknowledging that they have actually added new data. Kills two birds with one stone!
JP

More Answers (0)

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!