Export column name along with data in excel file in GUI

Helo,
I am builing a GUI and inside my GUI I have a table data named Results_table that contains a numerical values columnwise (n,1) stored in different columns with their respective names.
Now, I was able to export these values from the table Results_table in an excel file, but I could not find a solution to export the column names too with those values.
My code for this section is:
M=get(handles.Results_table, 'Data'); % I get values from table Results_table but it only exports values, and not their respective column names
FileName = uiputfile('*.xls','Save as');
xlswrite(FileName, M); %I save stored data in M
Is there a solution to this problem? How can I export column names that are associated with those numerical values inside table Results_table?
Thanks!

 Accepted Answer

M = get(handles.Results_table, 'Data');
Head = get(handles.Results_table, 'ColumnName');
Data = cat(1, Head, num2cell(M));
xlswrite(FileName, Data)

5 Comments

Hi Jan,
Thanks for your answer. I tested your code on a small sample and it worked fine, but when I apply it to my problem I get an error:
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in MY_GUI>OutputFol_Callback (line 592)
Data = cat(1, Head, num2cell(M));
What could be the problem?
My numerical results are stored in a table in columns (just like I tried on a simple example to test your code), so I am not sure where is the error.
Thanks!
The message means, that the number of ColumnName's does not equal the number of columns of the data. Maybe M is a cell already?
M = get(handles.Results_table, 'Data');
if ~iscell(M)
M = num2cell(M);
end
Hi Simon,
I tried that solution too, but no success. I think the problem lies in the ColumnName. Because when I export the M, it exports me the selected data from table Results_table.
You helped me in my previous problem where I stored data in table Results_table by only selecting desired data to be stored with their respected ColumnNames. See here
Now I tried to implement that same principle here (since I think, or I know, that the problem may lie in the ColumnName).
I tried to use:
M = get(handles.Results_table, 'Data');
Head1 = get(handles.Results_table, 'ColumnName', Head(Include)); %%I added Head(Include) which I declared globals in order to use them in this function, so that it only use the columnnames from the table _Results_table_
Data = cat(1, Head1, num2cell(M));
xlswrite(FileName, Data)
but I get an error as get does not allow me to add this extra line of code:
Error using get
Too many input arguments.
So I am not sure on how to proceed now.
I just used this line of code for retreiving Column names in another function (for further work), and saw that this code:
Head = get(handles.Results_table, 'ColumnName');
gives me the appropriate ColumnNames that matches the data in M, so now I am not sure where the error is regarding using cat.
I figured it out.
For some reason, the ColumnName that I extracted from handles.Results_table and stored it in Head had column names in a vertical order. That is why cat was giving me error, so by using:
Head1=transpose(Head)
and place them horizontaly, I was able to solve the problem.
Thanks!

Sign in to comment.

More Answers (0)

Asked:

on 7 Aug 2017

Commented:

on 8 Aug 2017

Community Treasure Hunt

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

Start Hunting!