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

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 commentaires

Did you try function writetable instead of xlswrite?
First of all hi Simon, thank you for replying this question.
For your question, I haven’t tried it yet. Is there any differences between these two?
xlswrite is not recommended from 2019a. On the other hand, writetable provides more options to write data on a file.
I see… so I have to change it into writable isn’t? But i didn’t know how to apply writable using gui 😅
Try this:
function pushbutton_save_Callback(hObject, eventdata, handles)
data = get(handles.uitable1, 'Data');
datax = get(handles.uitable1, 'ColumnName');
filename = 'testdata.xlsx';
T=array2table(data,'VariableName',datax);
writetable(T,filename);
end
Hi Mr. Chan, thank you for that coding. I have tried your solution, unfortunately it can't be used since there's errors.
The following is the errors stated:
Error using matlab.internal.tabular.private.varNamesDim.makeValidName (line 498)
'NO IC/PASSPORT' is not a valid variable name.
Error in matlab.internal.tabular.private.varNamesDim/validateAndAssignLabels (line 383)
[newLabels,wasMadeValid] = obj.makeValidName(newLabels,exceptionMode);
Error in matlab.internal.tabular.private.tabularDimension/setLabels (line 173)
obj = obj.validateAndAssignLabels(newLabels,indices,fullAssignment,fixDups,fixEmpties,fixIllegal);
Error in matlab.internal.tabular.private.tabularDimension/createLike_impl (line 355)
obj = obj.setLabels(dimLabels,[]);
Error in matlab.internal.tabular.private.varNamesDim/createLike (line 76)
obj = obj.createLike_impl(dimLength,dimLabels);
Error in tabular/initInternals (line 212)
t.varDim = t.varDim.createLike(nvars,varnames); % error if invalid, duplicate, or empty
Error in table.init (line 332)
t = t.initInternals(vars, numRows, rowLabels, numVars, varnames);
Error in array2table (line 64)
t = table.init(vars,nrows,rownames,nvars,varnames);
Error in PPNJ_TEST>pushbutton_save_Callback (line 431)
T=array2table(data,'VariableName',datax);
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
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.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

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:
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.

Connectez-vous pour commenter.

Plus de réponses (1)

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 commentaires

Hi Mr. @dpb, thank you so much for your willingness to explain and help me to solve this gui. I have tried your suggestion but it is turned out error 'Undefined function 'write' for input arguments of type 'cell'.'
dpb
dpb le 11 Juin 2022
Modifié(e) : dpb le 11 Juin 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.
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by