xlswrite error; not working entirely or throwing up an error

Hi All,
I'm getting the following error when using xlswrite:
Error using xlswrite (line 224)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Microsoft Excel cannot access the file 'D:\CAF\ash test march 2020\Test Piece 6\'. There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
Help File: xlmain11.chm
Help Context ID: 0
The code is as follows:
filename = [RawImages '\' Sample '\' Sample]; %Creating the filename to put the excel file in the appropriate folder
if isempty(TestPieceName)
xlswrite(filename, TestPiece);
else
xlswrite(filename, TestPiece, TestPieceName);
end
I find that if I change the filename statement to:
filename = [RawImages '\' Sample]; %Creating the filename to put the excel file in the appropriate folder
then it eliminates the error, but now xlswrite will simply run but not create the files. I've tried making "desktop" directories in system32 and 64 as per other suggestions on the site, and checked that the license for Office is working.
Any ideas how to fix this please?
Thanks

 Accepted Answer

Your variable Sample is empty, so when you construct
filename = [RawImages '\' Sample '\' Sample]
you are getting the result
D:\CAF\ash test march 2020\Test Piece 6\
based upon RawImages being D:\CAF\ash test march 2020\Test Piece 6 to which you append \ then empty then \ then empty, leaving you with something ending in \ which is not a valid file name to write to.
If Sample were not empty, then it would appear twice in D:\CAF\ash test march 2020\Test Piece 6\ which does not happen.

7 Comments

Hi Walter,
According to the workspace, "Sample" stores the string "Test Piece 6"
If so then
filename = [RawImages '\' Sample '\' Sample]
would have to give the result
D:\CAF\ash test march 2020\Test Piece 6\Test Piece 6
but Excel is saying that it cannot access file
D:\CAF\ash test march 2020\Test Piece 6\
By the way, you should be doing
rd = RawImages;
if ~isdir(rd); mkdir(rd); end
sdir = fullfile(rd, Sample);
if ~isdir(sdir); mkdir(sdir); end
filename = fullfile(sdir, Sample);
Question: are you expecting xlswrite() to default to .xls or to .xlsx or to .csv ? As you are not specifying the file extension.
Thanks Walter,
I see your point but I'm uncertain why it does this. The workspace shows the variable as not being empty!
I've tried writetable and writematrix instead but they also produce no output.
Regarding extension, I think I'd be satisfied with just having an output for now!
Did you try the series of mkdir() I show?
Hi Walter,
yeah, I've just implemented that but it stil refuses to create a file of the outputs!
Perhaps
rd = RawImages;
if ~isdir(rd); mkdir(rd); end
sdir = fullfile(rd, Sample);
if ~isdir(sdir); mkdir(sdir); end
filename = fullfile(sdir, [Sample '.xlsx']);
What is your current error message?
that's done the trick, thank you!

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!