Error can't load mat file from script, while manual loading is not an issue

9 Ansichten (letzte 30 Tage)
I've been trying to load a .mat file through a script and even used the generate script function again to see if I made any coding errors.
When I load it manually via the import data button it works fine.
However when I try to load it through a script (even matlabs own generated script) then it throws out the following error:
Error using load
Unable to read MAT-file C:\Users\vdvoor45\OneDrive -
Not a binary MAT-file. Try load -ASCII to read as text.
This is matlab's generated script when manually importing this EXACT SAME FILE
function importfile(fileToRead1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 28-Mar-2019 18:01:45
% Import the file
newData1 = load('-mat', fileToRead1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
This script still throws the exact same error as I was having.
I also am completely sure that the file is a proper .mat file
Also if this has to do with the lenght of the name/path then please give me a workaround instead of telling me to shorten the name.
If it happens to because of length then that should be considered a bug and fixed as well in my opinion.
I have a lot of files, over a 1000 which I need to keep track of what is what and the name is part of that system.
  1 Kommentar
Walter Roberson
Walter Roberson am 28 Mär. 2019
Visually it looks like the path might exceed 260 characters which is the Windows limit unless UNC is used.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Mär. 2019
Bearbeitet: Walter Roberson am 29 Mär. 2019
Maximum Path Length Limitation
In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character.
Your particular path is 261 characters excluding any null character (so 262 including the null).
You have reached a Windows limitation.
The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLengthparameter of the GetVolumeInformationfunction (this value is commonly 255 characters). To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".
You could try that.
  2 Kommentare
Bob photonics
Bob photonics am 29 Mär. 2019
Thank you very much for the quick reply and the help, that was indeed exactly the problem and solution.
You've made me very happy, thank you :)
Conrado Neto
Conrado Neto am 6 Jan. 2020
I had to log in here to give you the +1
I had the same issue and your solution fixed it
thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange





Community Treasure Hunt

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

Start Hunting!

Translated by