Error in BEAR toolbox initexcel copyfile(s​ourcefile,​destinatio​nfile)

Hi All,
When running the following piece of code from the BEAR toolbox from the european central bank:
delete([pref.datapath filesep 'results' filesep pref.results_sub '.xlsx']);
% then copy the blank excel file from the files to the data folder
sourcefile=[pwd filesep 'results.xlsx'];
destinationfile=[pref.datapath filesep 'results' filesep pref.results_sub '.xlsx'];
line 7: copyfile(sourcefile,destinationfile);
I get the following error
Error using copyfile
The system cannot find the path specified.
Error in initexcel (line 7)
copyfile(sourcefile,destinationfile);
However I downloaded the toolbox from the internet including all files in it thus I do not understand why it cannot find the files. I can find the results.xlsx file in the folder files.

Answers (1)

From the error message - "The system cannot find the path specified.", it seems to be a path issue.
Also, from "I can find the results.xlsx file in the folder files.", I assume that the file to be copied is present in the folder.
You can try these troubleshooting steps -
  • Verify Paths: Ensure that both sourcefile and destinationfile paths are correct. You can do this by printing them out before the copyfile command:
disp(sourcefile);
disp(destinationfile);
  • Check Current Directory: Make sure that the current directory (pwd) is what you expect it to be. Sometimes, the script might be running in a different directory than you think.
  • Existence of Source File: Confirm that the results.xlsx file actually exists in the directory returned by pwd.
if exist(sourcefile, 'file') ~= 2
error('Source file does not exist.');
end
  • Permissions: Ensure that you have the necessary permissions to read from the source directory and write to the destination directory.
  • Create Destination Directory: If the destination directory does not exist, you might need to create it before copying the file.
  • Add the files and folders involved to the MATLAB search path before copying.
Refer the following links -

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 25 May 2021

Answered:

on 29 Aug 2024

Community Treasure Hunt

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

Start Hunting!