write different data types to excel sheet

Hello,
I have a small matrix of data that contains a column of real numbers, a column of integers and a column of cell data. It appears to be stored as cell data, but I would like to keep the format of real, integer and string (different lengths) when I write to an excel spreadsheet. Using writematrix works to write the cell data, but I can't change the data type after that.
line0 is my cell table, and I use:
- writematrix(line0,filename,"Sheet",3,Range="A2:C13")
I could use help with the ability to format the data when writing to the excel file

回答(2 个)

Use writetable instead.
load Variable
T=table(str2double(line0(:,1)), uint32(str2double(line0(:,2))), line0(:,3), 'Var',CCHdr )
T = 12×3 table
Amount ($) Cost Center CostCenter Name __________ ___________ ______________________ 4.8292e+05 10110 "AdCom" 0 10115 "EXCOM" 0 10120 "FINCOM" 9889.8 10125 "Technical Committee" 0 10130 "Education" 3.327e+05 10135 "Membership" 7030.7 10140 "Awards" 0 10150 "Standards" 81263 10155 "Chapters" 49454 10160 "Other Committee" 22040 20130 "Publication Support" 38579 21020 "Meetings/Conferences"
writetable(T,'out.xlsx')
"It appears to be stored as cell data"
It is clearly stored as a string array:
S = load('Variable.mat')
S = struct with fields:
CCHdr: {'Amount ($)' 'Cost Center' 'CostCenter Name'} line0: [12×3 string]
S.line0
ans = 12×3 string array
"482918.41" "10110" "AdCom" "0" "10115" "EXCOM" "0" "10120" "FINCOM" "9889.78" "10125" "Technical Committee" "0" "10130" "Education" "332699.87" "10135" "Membership" "7030.74" "10140" "Awards" "0" "10150" "Standards" "81263.02" "10155" "Chapters" "49454.02" "10160" "Other Committee" "22040.27" "20130" "Publication Support" "38579.42" "21020" "Meetings/Conferences"
T = array2table(S.line0, 'VariableNames',S.CCHdr);
T = convertvars(T,[1,2],@double)
T = 12×3 table
Amount ($) Cost Center CostCenter Name __________ ___________ ______________________ 4.8292e+05 10110 "AdCom" 0 10115 "EXCOM" 0 10120 "FINCOM" 9889.8 10125 "Technical Committee" 0 10130 "Education" 3.327e+05 10135 "Membership" 7030.7 10140 "Awards" 0 10150 "Standards" 81263 10155 "Chapters" 49454 10160 "Other Committee" 22040 20130 "Publication Support" 38579 21020 "Meetings/Conferences"
writetable(T,'myexcel.xlsx')

类别

帮助中心File Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

版本

R2024a

提问:

about 21 hours 前

回答:

about 21 hours 前

Community Treasure Hunt

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

Start Hunting!

Translated by