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

조회 수: 38 (최근 30일)
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
Walter Roberson
Walter Roberson 2022년 6월 11일
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 2022년 6월 11일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 6월 11일
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
Aqilah Ahmad
Aqilah Ahmad 2022년 6월 12일
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 2022년 6월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2022년 6월 11일
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
dpb
dpb 2022년 6월 11일
편집: dpb 2022년 6월 11일
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 2022년 6월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by