How to save vectors of different lengths to excel file

17 views (last 30 days)
I have several one-column vectors of different lengths that I need to save to an existing Excel file. Each vector needs to be saved to a specific set of rows. I can accomplish this by using the xlswrite function with each vector (see code below). However, this results in the excel file being opened and closed every time a vector is saved, thereby taking a long time to finish.
Is there a way to combine all the different length vectors into a single vector so that I can just use xlswrite once? Or is there a way to creat a cell array that can do this?
%example of vectors to be saved to excel:
max_press = 32;
max_temp = 25;
velocities = [50 32 65 78.1 95 125 135 138];
Syst_Temps = [23 35 120 250 210];
Syst_Pressures = [50 32 31 21 35 34 35 21 18 21];
filename = 'Syst_Outputs.xlsx'; %Excel file to write to
sheet = 'System'; %Excel worksheet to write to
xlswrite(filename,max_press,sheet,'B10');
xlswrite(filename,max_temp,sheet,'B11');
xlswrite(filename,velocities,sheet,'B12:B18');
xlswrite(filename,Syst_Temps,sheet,'B21:B25');
xlswrite(filename,Syst_Pressures,sheet,'B27:B36');

Accepted Answer

Simon Chan
Simon Chan on 10 Aug 2021
Edited: Simon Chan on 10 Aug 2021
Since you are writing the data in a column, so you just need to take the transpose as follows.
Noticed that 'velocities' has 8 data, so it should fill up B12 to B19.
A=[NaN(9,1);max_press;max_temp;velocities';NaN(1,1);Syst_Temps';NaN(1,1);Syst_Pressures'];
writematrix(A,'Syst_Outputs.xlsx','Sheet','System','Range','B1')

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!