Using XLSWRITE with multiple arrays?

5 views (last 30 days)
Seán Gorman
Seán Gorman on 25 Nov 2017
Answered: Walter Roberson on 26 Nov 2017
I have a bunch of arrays which i need to put into an excel spreadsheet. There are 3 and they have to be put into column A, B and C respectively. How do i do this? xlswrite('CarData.xlxs','Sheet1','Model') tried this was just a mess.
>> load carbig.mat
>> Model=xlsread('Model')
Error using xlsread (line 139)
XLSREAD unable to open file 'Model'.
File 'Model' not found.
>> xlsread('Model')
Error using xlsread (line 139)
XLSREAD unable to open file 'Model'.
File 'Model' not found.
>> xlswrite('CarData.xlxs','Model')
>> xlswrite('CarData.xlxs','Sheet1','a','Model')
Warning: Added specified worksheet.
> In xlswrite>activate_sheet (line 300)
In xlswrite/ExecuteWrite (line 266)
In xlswrite (line 220)
Error using xlswrite (line 226)
The specified data range is invalid or too large to write to the specified file format. Try writing to an XLSX file and use Excel
A1 notation for the range argument, for example, A1:D4.
>> xlswrite('CarData.xlxs','Sheet1',,'Model')
xlswrite('CarData.xlxs','Sheet1',,'Model')
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
>> xlswrite('CarData.xlxs','Sheet1','Model')
Warning: Added specified worksheet.
> In xlswrite>activate_sheet (line 300)
In xlswrite/ExecuteWrite (line 266)
In xlswrite (line 220)

Answers (2)

dpb
dpb on 26 Nov 2017
xlswrite('CarData.xlxs',[A B C])
presuming A, B, C are column vectors of same size as are the variables in the carbig dataset altho those are not their names so you'll needs must use the real ones.
One at at time could be
xlswrite('CarData.xlxs',A,1,'A1')
xlswrite('CarData.xlxs',B,1,'B1')
...
altho would be quite slow that way as xlswrite opens and closes the spreadsheet every time and the ActiveX/COM interface is quite overhead-intensive already.
Read/study the documentation more thoroughly to follow the required syntax.
  2 Comments
Image Analyst
Image Analyst on 26 Nov 2017
Edited: Image Analyst on 26 Nov 2017
Sean, one problem was that you put Model inside single quotes when you passed it to xlswrite - that made a string instead of sending the variable itself into xlswrite(). And all your character arrays, like Model and Origin etc., should be cell arrays, not character arrays, otherwise it will put each letter into its own cell in Excel.
dpb
dpb on 26 Nov 2017
Edited: dpb on 26 Nov 2017
Amongst others...none of the cases OP tried have the arguments in the order specified by the documentation, even accounting for misplaced/uneeded quotes. The data array/element argument follows immediately after the filename, subsequently with optional sheet, range.

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Nov 2017
According to the workspace summary, your Model is a 406 x 36 char array. The methods other people are showing will not be able to mix that together properly.
data = [cellstr(Model), num2cell(Model_Year), num2cell(MPG)]; %build cell array
xlswrite('CarData.xlxs', data, 'Sheet1') %data first then sheet

Community Treasure Hunt

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

Start Hunting!