Add a new row in an uitable dynamically using a pushbutton
8 views (last 30 days)
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.
0 Comments
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 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.
Julian
on 13 Aug 2014
Edited: Julian
on 13 Aug 2014
2 Comments
imene. B
on 11 Jul 2016
Edited: imene. B
on 11 Jul 2016
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);
See Also
Categories
Find more on Migrate GUIDE Apps 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!