How do I make a MATLAB created table properly appear in EXCEL?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm attempting to write a MATLAB created table to EXCEL using the following code;
%
% Write_Parts_Table.m
%
% Clear out all workspace variables and the command window
clear all;
clc;
% Assign data
Company = {'Company_A' 'Company_B'};
Run_Numbers = {'10000' '2500'};
Make = {{'Ford' 'Audi' 'Chevy'} {'Chevy' 'Volvo'}};
% Create table from workspace variables
Z = table(Company', Run_Numbers', Make', 'VariableNames',{'Company_Name' 'Run_Numbers' 'Make'});
% Disable the warnings for adding specified worksheets. These occur because
% EXCEL has a default value of 3 worksheets / file
warning('off', 'MATLAB:xlswrite:AddSheet');
% Write table to EXCEL
writetable(Z, '(A)Parts_Info.xls', 'Sheet', 1, 'Range', 'A3');
% For inspection, have Windows open the EXCEL workbook just created
winopen('(A)Parts_Info.xls');
When I view the variable Z, I get the following;
'Company_A' '10000' 1x3 cell
'Company_B' '2500' 1x2 cell
When EXCEL is opened, I see the following;
Company_Name Run_Numbers Make
Company_A 10000
Company_B 2500
But I expect to see this;
Company_Name Run_Numbers Make
Company_A 10000 Ford Audi Chevy
Company_B 2500 Chevy Volvo
It appears I have some kind of problem pertaining to the size of the data in the 'make' column.
Is this a case where I need to add more column headers? Or am I using the writetable incorrectly?
Thank you.
0 commentaires
Réponse acceptée
Kirby Fears
le 24 Mai 2016
Modifié(e) : Kirby Fears
le 24 Mai 2016
Brad,
Each row of Make is a collection of strings that cannot be concatenated automagically.
You can concatenate the strings yourself using a specified delimiter like this:
Company = {'Company_A' 'Company_B'};
Run_Numbers = {'10000' '2500'};
Make = {{'Ford' 'Audi' 'Chevy'} {'Chevy' 'Volvo'}};
% Create table from workspace variables
Z = table(Company', Run_Numbers', Make', 'VariableNames',{'Company_Name' 'Run_Numbers' 'Make'});
% Reassign Z.Make with concatenated strings
% I'm using a space to separate each string.
% You can use other characters like ',' or ';'
Z.Make = cellfun(@(c)delimcat(c,' '),Z.Make,'UniformOutput',false);
% below is a function to concatenate strings.
% you can paste it as a subfunction into an
% existing function or you can make a file called
% delimcat.m and save it on your path
function c = delimcat(c,d)
c = [c;repmat({d},1,numel(c)-1),{''}];
c = [c{:}];
From here, writetable will output the concatenated Make strings.
Hope this helps.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!