How to export data displayed from uitable on GUI MATLAB to Microsoft Excel?

20 views (last 30 days)
Hello my dearest friends, it's me again.
I have a problem regarding my gui where i can't export data displayed from uitable on GUI MATLAB into Microsoft Excel. Frankly to say, I have tried plenty of codings but none of them works on mine because the columns' name only display a single name for straight 7 colums (loop) and the data for every rows is not being displayed. I am using push button (save) in order to export the data.
The following is my data from uitable GUI:
The following is my coding:
function pushbutton_save_Callback(hObject, eventdata, handles)
data = get(handles.uitable1, 'Data');
datax = get(handles.uitable1, 'ColumnName');
filename = 'testdata.xlsx';
xlswrite(filename,data);
xlswrite(filename,datax,1,'A1:G1');
end
The following is the result obtained:
Is there anyone willing to help me out? I would like to thank you for willing to spend your time for helping me.
  8 Comments
Walter Roberson
Walter Roberson on 11 Jun 2022
Which MATLAB version are you using? It must be a few years old, to not support those variable names. I suspect that it is too old to support writecell.
Also please confirm that you are using Windows and not MacOS
dpb
dpb on 11 Jun 2022
Ah! Indeed. Checking the doc; writetable appeared in R2013b while the corollary additions or writecell and friends didn't make their appearance on the scene until R2019a.

Sign in to comment.

Accepted Answer

Voss
Voss on 11 Jun 2022
Since it appears that your MATLAB version may be older than writecell and too old to make convenient use of writetable, here's how to do it using xlswrite:
function pushbutton_save_Callback(hObject, eventdata, handles)
data = get(handles.uitable1, 'Data');
datax = get(handles.uitable1, 'ColumnName');
filename = 'testdata.xlsx';
xlswrite(filename,[datax.'; data]);
end
  2 Comments
Aqilah Ahmad
Aqilah Ahmad on 12 Jun 2022
Hi Mr. @Voss, thank you for your willingness to assist me. Isn't this the third time you've come to assist me solve my GUI? I am delighted to see you back here and appreciative for your generous solution. I'd want to let you know that your suggestion is being implemented in my GUI. I've attached the results below. I'm really new to GUI Matlab, so I apologise for burdening everyone in this community with my problems. Last but not least, Mr. @Voss, I hope you can continue to assist others with Matlab and I pray that your day goes well.
My gui:
The result:
Voss
Voss on 12 Jun 2022
Hi @Aqilah Ahmad You're welcome! It's no burden at all; I am happy to help.
Your GUI is coming together nicely! All the best.

Sign in to comment.

More Answers (1)

dpb
dpb on 11 Jun 2022
If you use a table for the uitable content, then two advantages --
  1. The uitable looks just like the data table you're manipulating in code, and
  2. The returned 'Data' property is also a table directly writeable by writetable
With this presumption of converting your data structure in your code, then the callback function would be
function pushbutton_save_Callback(hObject, eventdata, handles)
tData=handles.uitable1.Data;
filename = 'testdata.xlsx';
writetable(tData,filename);
end
Read the details in the doc for xlswrite on it's limitations about what it does with other than pure numeric data to see why it didn't do what was expected; but as noted above, its use has been deprecated and writetable is much more capable for nonhomogeneous data.
  3 Comments
dpb
dpb on 11 Jun 2022
Edited: dpb on 11 Jun 2022
Well, it isn't write but writetable and if it is a cell array then either
  1. use cell2table to convert the cell array to the table for writetable or
  2. use writecell instead.
As I prefaced the Answer, I would still HIGHLY recommend to go back to the point at which your app creates the data to go into the uitable and turn it into a MATLAB table there. You will have some work to do to use the table elsewhere in referencing the table instead of a cell array, but undoubtedly the benefits will outweigh the effort in ease of coding going forward.
ADDENDUM:
As @Walter Roberson notes above, you'll have to have R2019a or later for option 2. above to work; with the unallowed variable name; you almost certainly are ealier; I don't recall when the extension to allos embedded blanks was introduced but several incarnations earlier than that, I'm sure.
That would make also potentially make the suggestion to use the table for the .Data property moot; that's a feature not available until R2018a.
But, writing cell array data to spreadsheet is, while possible, much more cumbersome if don't use writetable so I'd still suggest using it for the internal data structure and convert to cell array for the GUI table. Pain, but unless you can upgrade to release that supports the newer features, it appears to be unavoidable.
Aqilah Ahmad
Aqilah Ahmad on 12 Jun 2022
Mr. @dpb thank you for your suggestions. I really appreciate it as it works on my friend's gui but what a bummer it does not works on mine. But it is okay since your suggestion also can help other people. I am very glad that you willing to spend your time here to help me out. Please have a nice day and stay safe.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!