exportgraphics does not save at specific location

54 次查看(过去 30 天)
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).

采纳的回答

Felix Müller
Felix Müller 2022-7-1
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.

更多回答(1 个)

Jan
Jan 2022-6-29
编辑:Jan 2022-6-29
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 个评论
Felix Müller
Felix Müller 2022-6-29
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 2022-7-1
Thanks for your ideas! I found a solution and wrote an answer with it. (~ for home directory was the problem)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by