MATLAB Answers

xlswrite error; not working entirely or throwing up an error

33 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jan 2021
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

Show 4 older comments
Teshan Rezel
Teshan Rezel on 18 Jan 2021
Hi Walter,
yeah, I've just implemented that but it stil refuses to create a file of the outputs!
Walter Roberson
Walter Roberson on 18 Jan 2021
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?

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!