Error saving file: Cannot create '<filename>.mat' because '<fullpath>' does not exist.

40 views (last 30 days)
Hello,
I have tried every variation I can think of as well as Googled and cannot come up with a solution. I'm baffled as to why this is happening. Here's what's happening: I read in some files from my external hard drive, run them through my code, and then try to save the output back to my external hard drive. I have an absolute path "/Volumes/TOSHIBA EXT/...". It gets the input files just fine, but then it fails when I use the "save" function.
Specs: running R2011b on Mac OS X 10.6.8
Here's a bit of my code:
path_to_project_data = '/Volumes/TOSHIBA'' EXT''/preseis/Documents/Project'' Data''/' ;
matlab_save = '/Volumes/TOSHIBA'''' EXT''''/preseis/Documents/Project'''' Data''''/' ;
path_to_files2_3 = fullfile(path_to_project_data,'CyberShake/FFMs/FengABFSet/RuptureGenerator2_3/') ;
matlab_save2_3 = fullfile(matlab_save,'CyberShake/FFMs/FengABFSet/RuptureGenerator2_3/') ;
<do some stuff to create a structure variable "FFM">
name = FFM.event_id ; % a string event name identifier
eval(['save ''' matlab_save2_3 'MatlabFiles/FFM/' name '.mat'' FFM'])
----------
Output:
Error using save
Cannot create 'C218_0s0000h0000.mat' because '/Volumes/TOSHIBA' EXT'/preseis/Documents/Project'
Data'/CyberShake/FFMs/FengABFSet/RuptureGenerator2_3/MatlabFiles/FFM' does not exist.
This code does not work and I have tried the following variations:
String concatenation without using the "fullfile' function:
path_to_files2_3 = [path_to_project_data 'CyberShake/FFMs/FengABFSet/RuptureGenerator2_3/'] ;
Changing the save command:
eval(['save ' path_to_files2_3 'MatlabFiles/FFM/' name '.mat FFM'])
eval(['save ''' path_to_files2_3 'MatlabFiles/FFM/' name '.mat'' FFM'])
save([path_to_files2_3 'MatlabFiles/FFM/' name '.mat'],'FFM')
**as well as the above variations using matlab_save2_3 in place of path_to_files2_3
Changing the path_to_project_data variable:
path_to_project_data = '/Volumes/TOSHIBA EXT/preseis/Documents/Project Data/' ;
path_to_project_data = '/Volumes/TOSHIBA\ EXT/preseis/Documents/Project\ Data/' ;
path_to_project_data = '/Volumes/TOSHIBA'''' EXT''''/preseis/Documents/Project'''' Data''''/' ;
**as well as the above variations using matlab_save and every possible combination of the two together
I even typed in the command window:
save '/Volumes/..../<filename>.mat' FFM
*using* tabs to fill in the save string to make sure I wasn't mistyping anything...it still failed with the same message. I tried loading an existing file from the same directory and the "load" function works just file with the full file path I gave it. I am so confused and at my wits end.
Can someone please help me with this! Why won't "save" work, but "load" and my "system" calls work with these full file paths I've given??? Please and thank you!
  4 Comments
Ramkumar
Ramkumar on 28 Sep 2013
Another probable reason is that you do not have 'write' access to the location where you want to write the file. This has happened to me before. The code which ran perfectly in my laptop and was able to save the file was not able to do so in my office computer. It gave me the same error you have mentioned. Then I realized that I was saving the file to a location where I did not have permission to save.
Check for this also..
Ram
Stephen23
Stephen23 on 31 Mar 2015
Edited: Stephen23 on 22 May 2020
Why on earth are you using eval just to call save ? Do NOT do this !
That practically guarantees that you will have buggy undebuggable code.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 29 Sep 2013
Why the heck are you doing this at all, with eval(), instead of calling save() as a function?
A_path = 'some string';
save(A_path, 'the_variable');
If you need EXT and whatever to be substituted in dynamically, then use sprintf to construct the file name:
series_name = 'CyberShake/FFMs/FengABFSet/RuptureGenerator2_3';
experiment_name = 'XY034'
A_path = sprintf('/your/base/directory/%s/%s.mat', series_name, experiment_name);
save(A_path, 'the_variable');

Siluveru Kiran Kumar
Siluveru Kiran Kumar on 10 Apr 2018
If you are using ubuntu os run matlab with root mode i.e in the terminal sudo ./matlab

Arjun Sahdev
Arjun Sahdev on 6 Dec 2018
I had the same problem while I was saving a string array in .mat format. The problem was that you have to give the filename accesible within the current operating directory. The operating directory is where your code is saved. Then moment I did that it worked. Another reason might be that you are speciying the path incorrectly.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!