Add a new row in an uitable dynamically using a pushbutton
이전 댓글 표시
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.
답변 (2개)
Joseph Cheng
2014년 8월 13일
편집: Joseph Cheng
2014년 8월 13일
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
Joseph Cheng
2014년 8월 13일
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
2014년 8월 19일
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
2018년 3월 25일
thanks Joseph Cheng
댓글 수: 2
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
2019년 10월 7일
How to add coloumn of 0 zeroes?
카테고리
도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!