exportgraphics does not save at specific location

51 views (last 30 days)
I have a figure that I want save with exportgraphics(), but I get an error about the path.
>> str_save = '~/Documents/MATLAB/A/B/C/test_file_name.pdf';
>> isfolder('~/Documents/MATLAB/A/B/C/')
ans =
logical
1
>> exportgraphics(fig, str_save)
Error using exportgraphics
Problem while processing in an OutputHelper. ~/Documents/MATLAB/A/B/C/test_file_name.pdf (No such file or directory)
com.mathworks.hg.util.OutputHelperProcessingException: Problem while processing in an OutputHelper. ~/Documents/MATLAB/A/B/C/test_file_name.pdf (No such file or directory)
at com.mathworks.hg.util.HGVectorOutputHelper.open(HGVectorOutputHelper.java:76)
Caused by: com.mathworks.hg.print.OutputProcessingException: ~/Documents/MATLAB/A/B/C/test_file_name.pdf (No such file or directory)
at com.mathworks.hg.print.BaseVectorStrategy.open(BaseVectorStrategy.java:30)
at com.mathworks.hg.util.HGVectorOutputHelper.open(HGVectorOutputHelper.java:66)
If I save it to the current working directory
str_save = './test_file_name.pdf';
it works. When I save it with print() instead of exportgraphics() it also works.
Adding the output folders to the current path (via GUI or code) does not change the error.
Could this have to do with write access? But checking the rights with fileattrib() says I have write access (which should be the case since the folder is created in my script with 774 rights).

Accepted Answer

Felix Müller
Felix Müller on 1 Jul 2022
I have now "solved" my problem. When I do not use the tilde (~) in my path to represent the home folder but instead use a full path, it works. Then exportgraphics saves the file as I want.
I am now exactly sure why this is the case. Maybe it does not expand the tilde properly and thus thinks the directory does not exist? (But why wouldn't it?)
Final line: When using exportgraphics provide a full path to the save location and do not use the tilde for home directory.

More Answers (1)

Jan
Jan on 29 Jun 2022
Edited: Jan on 29 Jun 2022
The folder is existing but Matlab cannot create a file in it. Then Matlab does not have write permissions in this folder.
fileattrib('~/Documents/MATLAB/A/B/C/')
Allow the current user to create files in this folder.
  4 Comments
Felix Müller
Felix Müller on 29 Jun 2022
I get no output from your code. fid is equal to 3 and msg is a 0×0 empty char array.
That is not the real path, I just created that folder structure for testing. But there are only letters, numbers and slashes in the real path (all numbers converted to strings etc). And the print() function actually can save the same figure to the same path. So Matlab can write stuff there, I just get the error using exportgraphics.
Felix Müller
Felix Müller on 1 Jul 2022
Thanks for your ideas! I found a solution and wrote an answer with it. (~ for home directory was the problem)

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!