write table to .xls document with variable (cell array) not splitting into different cells

5 views (last 30 days)
Hi,
I am having some trouble with writetable into a .xls document and would like to ask for your help.
I have a table A with many variables as below.
A = [Case Load_Type elements X1 PX1 PY1 PZ1 X2 PX2 PY2 PZ2 glob np relative al be ga memo]
Please see pic below and find the table attached.
when I write it to excel I use
writetable(A,'Table.xls','Sheet',1,'Range','A1')
All cells are correctly written except for elements which uses a single separate cell ,column for each number.
I would like to get all elements into one cell as below
How could I define the elements column in matlab so that it keeps all numbers for each single (row,column) separated by a space and in the same cell when writing table to .xls?
Thank you in advance.
  2 Comments
Guillaume
Guillaume on 26 Jul 2019
Excel does not support having several numbers in the same cell, hence matlab distributes the numbers in contiguous cells.
The alternative is converting the numbers to text and concatenate the textual representations with a suitable separator (space?, comma?) so that they can be written into the same cell. The value of that is dubious since excel will treat the content of the cells as text so won't be able to perform any useful calculation on them.
If that's what you want, it can be done easily but this is just using a complicated program (excel) as just a basic viewer for tabular data. You my as well write the table as a text file.
Ana Bermejo Jimenez
Ana Bermejo Jimenez on 26 Jul 2019
Hi Guillaume,
Thanks a lot for your answer
Yes, I would not have to do anything else with the data afterwards, so I could do with converting numbers to text and concatenate.
Should I use num2str or char ?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 26 Jul 2019
Edited: Guillaume on 26 Jul 2019
You can convert your elements column into text with:
A.elements = cellfun(@num2str, A.elements, 'UniformOutput', false);
If you want to better control the separator (num2str may put too much space if the numbers are not integer):
separator = ' '; %or ',', or whatever you want
A.elements = cellfun(@(v) strjoin(arrayfun(@num2str, v, 'UniformOutput', false), separator), A.elements, 'UniformOutput', false);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!