Why does "readtable" fail to read a file from OneDrive?

37 views (last 30 days)

I am using OneDrive for storing my Microsoft Excel spreadsheets. I noticed one issue when trying to read data from it:

  • When my spreadsheet is not open by any application, then MATLAB successfully loads the data from the spreadsheet.
  • When my spreadsheet is open in Microsoft Excel, then MATLAB fails to read data from it using "readtable". The error message is below.
>> readtable('mydata.xlsx', 'UseExcel', true)
Unable to open file 'C:\Users\<user>\OneDrive ....' as a workbook. Check that the file exists, read access is available, and the file is a valid spreadsheet file.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Feb 2020
The issue is likely to be with OneDrive locking the file for editing purposes, although our development team is aware of the issue and investigating.
For now, the workaround is to close the application which uses the file in OneDrive, and then use MATLAB to load the file. Alternatively, the file can be moved or copied to a local directory, ensuring OneDrive does not lock it.

More Answers (1)

Jeno Boka
Jeno Boka on 3 May 2022
While the issue does not get fixed, the the following workaround
Use the system() function to copy with an operating system command e.g. copy. This way you don't have to restart MATLAB.
See the code example below:
function copyFromOneDrive(source, dest)
exitCode = system(sprintf('copy "%s" "%s"', source, dest))
assert(exitCode == 0, 'File copy failed');
end

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!