Add a new row in an uitable dynamically using a pushbutton
Show older comments
Hello everybody
I'm creating an app for making electrical calculations but I have to add rows constantly for entrying new data to be evaluated. I'm using a code i saw on a MatLAB tutorial on Youtube but it doesn't run as I was expected. I would like some help to improve this code and make it works properly.
The code I'm using 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.uitable1, 'data');
data=str2double(data);
data(end+1,:)=0;
set(handles.uitable1, 'data', data);
I appreciate for your help.
Answers (2)
Joseph Cheng
on 13 Aug 2014
Edited: Joseph Cheng
on 13 Aug 2014
what type of stuff are you sticking in this table? What are you expecting? if the data variable (place a breakpoint at data and see) is a string of numbers? letters?
is data at the get(handles.uitable1,'data') a cell array? if so i wouldn't convert data from a str2double as it could mess up your data depending on whats there. i would just go
data=get(handles.uitable1, 'data');
data(end+1,:)={0}; %if data is a cell or
data(end+1,:)=0; %if data is an array.
set(handles.uitable1, 'data', data);
4 Comments
Joseph Cheng
on 13 Aug 2014
So, just like you do the set(handles.uitable1,'data',data) you can set the 'RowName' and 'ColumnName' just the same.
http://www.mathworks.com/help/matlab/ref/uitable.html is a link to all the parameters or examples. it doesn't do it in guide but you should get a feel of what you can do. so intead of using uitable_____ to create it, GUIDE has already created and has the handle of handles.uitable already.
so in the example you can set the row and column names by doing something like this
rnames = {'First','Second','Third'};
set(handles.uitable1,'RowName',rnames);
and append as you would like the data when you create something new.
Joseph Cheng
on 19 Aug 2014
Has by answers resolved your initial question? if so can you mark it as answered?
To answer your question below i would just convert the cell type into a mat then do the str to number conversion or vice versa. I don't have a sample of your cases so you'll have to check out the different functions and how to perform the conversion.
Stephen23
on 25 Mar 2018
thanks Joseph Cheng
2 Comments
thank you very much julian :D
here's my code, it works perfectly
Add a row
% --- 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.uitable1, 'data');
data(end+1,:)={0};
set(handles.uitable1, 'data', data);
Anas Sheikh
on 7 Oct 2019
How to add coloumn of 0 zeroes?
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!