How do I make a MATLAB created table properly appear in EXCEL?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Kirby Fears
2016 年 5 月 24 日
編集済み: Kirby Fears
2016 年 5 月 24 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!