Export Cell-Data to .xlsx [Saves less data]
Mostrar comentarios más antiguos
Hello Everyone,
I am using following code to export Cell-data to .xlsx file.
filename='(AutoCheck7000).xlsx';
output = [header; Dataset];
xlswrite(filename,output); % Write to the new excel file.
Where as;
header is cell with 1*13 size
dataset is cell with 189*13 size
I have concentrated them using a variable output but xlswrite is only writing 2 header names instead of all names.
For instance;
the result is:

Whereas, the output-variable is as follows, so .xlsx should show all header names as well:

Any help would be appreciated :-)
Best regard,s
Waqar Ali Memon
3 comentarios
Stephen23
el 17 de Jul. de 2019
Waqar Ali Memon
el 17 de Jul. de 2019
Waqar Ali Memon
el 17 de Jul. de 2019
Respuesta aceptada
Más respuestas (1)
Peter Jarosi
el 17 de Jul. de 2019
Actually, your Dataset and header need some massage before converting to table:
filename='(AutoCheck7000).xlsx';
load('Dataset.mat');
Dataset = cellfun(@num2str, Dataset, 'un', 0);
load('header.mat');
header = cellfun(@char, header, 'un', 0);
header = strrep(header, '02', 'file_02');
header = strrep(header, ' ', '_');
header = strrep(header, '.', '_');
header = strrep(header, '-', '_');
header = strrep(header, '(', '_');
header = strrep(header, ')', '_');
header = strrep(header, '[', '_');
header = strrep(header, ']', '_');
myTable = cell2table(Dataset, 'VariableNames', header);
writetable(myTable, filename, 'WriteVariableNames', true);
It'll work for you. I've tested.
2 comentarios
Waqar Ali Memon
el 17 de Jul. de 2019
Peter Jarosi
el 17 de Jul. de 2019
Editada: Peter Jarosi
el 17 de Jul. de 2019
Massaging header was needed, because it violated the rules of variable names. Quote "A valid variable name begins with a letter and contains not more than namelengthmax characters. Valid variable names can include letters, digits, and underscores." End quote. https://www.mathworks.com/help/matlab/ref/isvarname.html
Your header was in cells of cells format instead of cells of strings format, that's why I applied
header = cellfun(@char, header, 'un', 0);
Your dataset contained mixed numeric and string cells, so I converted all of them to strings.
Dataset = cellfun(@num2str, Dataset, 'un', 0);
Usually it's not necessary but it seemed to be the easiest way to fix your problem.
You may also accept my other answer, because that is more general, so that it might help others to solve their problems (and helps me to achieve rising star badge :-))
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!