MATLAB Answers

Leon
0

How to combine string and numbers before using xlswrite to export data into an Excel file?

Asked by Leon
on 17 Mar 2019
Latest activity Commented on by Leon
on 18 Mar 2019
Accepted Answer by KSSV
I have like to write the below information into one Excel spreadsheet:
Cruise # Longitude Latitude Year Month Day
SE_01 -126 35 2017 9 17
SE_01 -123 31 2017 9 18
SE_01 -121 27 2017 9 20
How do I form the matrix? For Column 1, if it is numerical, I would do: A*size(3,1); but how about strings? Once I get the string column for Column 1, how do I combine it with the other numerical matrix values?
Many thanks!

  2 Comments

I would recommend using table variable to store your data, and writetable function to save it as an Excel file. Please refer to the following documentation pages.
Many thanks for the reply! I really appreciate it.

Sign in to comment.

Tags

Products

1 Answer

Answer by KSSV
on 18 Mar 2019
 Accepted Answer

cruise = {'SE_01', 'SE_01','SE_01'}' ;
Longitude = [-126 -123 -121]' ;
Latitude = [35 31 27]' ;
Year = [2017 2017 2017]' ;
Month = [9 9 9 ]';
Day = [17 18 20]' ;
thedates = datetime(Year,Month,Day) ;
T = table(cruise,thedates,Longitude,Latitude);
writetable(T,'test.xlsx')

  3 Comments

Many thanks for the sample code! It works great.
For future reference, the cruise column can also be generated this way:
[cruise{1:3}] = deal('SE_01');
cruise = cruise';

Sign in to comment.