In a matlab script printing multiple figures, I am trying to save as each figure as a .png image. I am doing so by using the print function. When testing the print function, I get the following:
>> print('newfile','-dpng')
Error using name (line 102)
Cannot create output file '.\newfile.png'.
Error in print (line 200) pj = name( pj );
Could anyone tell me how to fix this?
Thanks a lot.

 채택된 답변

Jan
Jan 2018년 1월 23일

5 개 추천

The error message means, that the file "newfile.png" cannot be created in the current folder. Either this file is existing already, but write-protected or in use. Or you do not have write permissions in the current folder.
Prefer to define file names including the complete path, because you cannot control if a timer or GUI callback uses cd to change the current folder. Relying on the current folder to be, where it is expected, is not reliable.

댓글 수: 3

fawkok
fawkok 2018년 1월 23일
Thanks a lot! With the complete path it is working. I have a follow-up question: is it possible to include in the filename a variable. Say, I print 10 graphs iteratively and want each graph to be called 1, 2, 3, etc. How do I do this? Is it possible with the num2str function?
Walter Roberson
Walter Roberson 2018년 1월 23일
Yes you can use num2str and concatenation, but it is usually better to use sprintf and fullfile()
Jan
Jan 2018년 1월 23일
편집: Jan 2018년 1월 23일
for k = 1:10
file = fullfile(folder, sprintf('File%03d.jpg', k));
disp(file);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2018년 1월 23일

편집:

Jan
2018년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by