How do I save a file name as a string? hFig is the Figure I want to save and str is the name I want to save this figure as. When I run it i get the below message:
Error using saveas (line 96)
Invalid filename.
str = 'ERP_GRP_1'
saveas(hFig, fullfile(path, [str '.jpeg']));

 채택된 답변

cr
cr 2021년 12월 14일

0 개 추천

saveas(hfig,[directoryPath, str, '.jpeg'])
directoryPath is the string with ending in fileseperator / or \ depending on your OS. If your directory path string doesn't end in it you may include filesep() in the brackets.

댓글 수: 4

Thanks. I tried this but I get this error. I tired adding a separator but same error.
Error using saveas (line 96)
Invalid filename.
directoryPath=fullfile('*\Desktop\EEG figures');
saveas(hFig,[directoryPath, str, '.jpeg'])
cr
cr 2021년 12월 14일
편집: cr 2021년 12월 14일
I'm not sure if you need fullfile() here. Just use the path c:\user\Desktop\EEG figures\. I'd presume the line you are executing in your Matlab doesn't contain " * ".
DavidL88
DavidL88 2021년 12월 14일
That worked thanks.
You can still use fullfile, you just need to assemble the file name separately.
directoryPath='*\Desktop\EEG figures';
saveas(hFig, fullfile(directoryPath, [str, '.jpeg']))
But the fact that your file path starts with a wildcard seems problematic. I also would recommend against storing your data on the Desktop; sure it's convenient to access manually by clicking on the folder on the desktop, but it makes it more difficult to access the data programmatically.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 12월 14일

0 개 추천

If you have r2020b or later you should use exportgraphics().
And you should not use JPG format unless you're willing to have horrible jpeg compression artifacts.
str = 'ERP_GRP_1';
% Create filename with folder prepended.
folder = pwd; % Or whatever you want.
fullFileName = fullfile(folder, [str, '.png']);
% Save hFig
exportgraphics(hFig, fullFileName);

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

질문:

2021년 12월 14일

답변:

2021년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by