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

11 views (last 30 days)
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 -
imec\Lumerical\Pix4Life_simulations\Fluorescence_nanopore_WG_coupling\Clean_NP_in_WG_with_dipole\Mesh_convergence_tests\Output_Files_converge_X_disttoPML\WG0001_Trans_and_Purcell_mpar_1_mval_1_waterSub_x0_y0_z0_NPdia50_WL640_660.mat.
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)
%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}));
end
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 Comment
Walter Roberson
Walter Roberson on 28 Mar 2019
Visually it looks like the path might exceed 260 characters which is the Windows limit unless UNC is used.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2019
Edited: Walter Roberson on 29 Mar 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 Comments
Bob photonics
Bob photonics on 29 Mar 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 on 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

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!