Xlswrite and copy&paste uitable column names

Hi,
I have a uitable which I copy&paste and export. It does not paste the uitable column names to Excel. How can I do it?. Do I have to change something in the property inspector?
In addition, how do I also export also the uitable column names when I am saving the uitable data (using xlswrite).
Thanks

 Accepted Answer

Jan
Jan on 17 Feb 2013
Copy&Paste duplicates the marked contents, but the headers are not selected. Therefore I'd suggest am extra button or adding a context menu on the Java level. From there you can apply a method similar to Azzi's suggestion considering if the Data are stored as cell or double matrix.

More Answers (1)

col=get(t,'columnname')' % t is the handle of your table
data=get(t,'data')
num=[col;data]
xlswrite('file.xls',num)

5 Comments

This works, when the uitable's data are a cell. But it can be a double matrix also.
Yes. In this case
num=[col;num2cell(data)]
Thanks. Works like magic.
Something strange happen when I run this pushbutton in my GUI.
function exportButton_Callback(hObject, eventdata, handles)
% hObject handle to exportButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]= uiputfile('*.xls','Save as');
col=get(handles.infoTable,'columnname')' ;
data=get(handles.infoTable,'data');
num=[col;data];
xlswrite([pathname filename],num);
If the file already exsits it does not overwrite it but just repleace the rows I have. To be clear, if I have an old xls file with 3 rows and the new uitable export is just 2 rows, the new file overwrite the 2 rows but will not erase the 3 row. Any idea why?
Thanks.
You can delete your file before saving again.
delete([pathname filename])

Sign in to comment.

Asked:

on 17 Feb 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!