I am trying to figure out how to use writetable instead of xlswrite, but the feature is converting my numbers to text. Headers are text.

3 views (last 30 days)
Original Code that works fine
Excel_Sheet = [Excel_head; Excel_out];
xlswrite('C:\D_Local\MATLAB\AAAA\PhaseII_CA_out.xlsx',Excel_sheet);
Where Excel_head is a text array and Excel_out is a numberical matrix of values.
New Code that converts the numbers to text
Excel_sheet = table([Excel_head; Excel_out]);
writetable(Excel_sheet,'C:\D_Local\MATLAB\AAA\PhaseII_CA_out.xlsx');
why does it convert? There must be a way of making the table with numbers?
Best regards,
Farley

Accepted Answer

Farley Postgate
Farley Postgate on 31 Jul 2024
So what I found to work was to separate the command into two commands, one for the header text and one for the numerical data.
filename = 'C:\CA_SIM\xxx\CA_out.xlsx';
writematrix(Excel_head,filename,'Sheet','Data1','Range','A1');
writematrix(Excel_out,filename,'Sheet','Data1','Range','A2');
This replaces
Excel_sheet = [Excel_head; Excel_out];
xlswrite('C:\CA_SIM\CA_out.xlsx',Excel_sheet);
I tried all the suggestions but this is the only way I found to get a clean excel spreadsheet with numberical data under text headers.
Thanks for the help,
Farley Postgate

More Answers (2)

Voss
Voss on 9 Jul 2024
Excel_Sheet = array2table(Excel_out,'VariableNames',Excel_head);
writetable(Excel_sheet,'C:\D_Local\MATLAB\AAA\PhaseII_CA_out.xlsx');

Steven Lord
Steven Lord on 9 Jul 2024
Don't concatenate Excel_head and Excel_out together. Store them as separate table variables.
load patients;
LNS = string(LastName);
T = table(LNS, Age, VariableNames = ["Last Name", "Age"]);
head(T)
Last Name Age __________ ___ "Smith" 38 "Johnson" 43 "Williams" 38 "Jones" 40 "Brown" 49 "Davis" 46 "Miller" 33 "Wilson" 40
Compare with the concatenation code, which turns the numeric Age variable into text:
T2 = table([LNS, Age], VariableNames = ["Last Name and Age"]);
head(T2)
Last Name and Age __________________ "Smith" "38" "Johnson" "43" "Williams" "38" "Jones" "40" "Brown" "49" "Davis" "46" "Miller" "33" "Wilson" "40"

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!