exportgraphics does not save at specific location

조회 수: 13 (최근 30일)
Felix Müller
Felix Müller 2022년 6월 29일
댓글: Felix Müller 2022년 7월 1일
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에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by