How to export data displayed from uitable on GUI MATLAB to Microsoft Excel?
20 views (last 30 days)
Show older comments
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
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
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.
Accepted Answer
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
More Answers (1)
dpb
on 11 Jun 2022
If you use a table for the uitable content, then two advantages --
- The uitable looks just like the data table you're manipulating in code, and
- 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
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
- use cell2table to convert the cell array to the table for writetable or
- 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.
See Also
Categories
Find more on Spreadsheets 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!