Save figure name as string

7 ビュー (過去 30 日間)
DavidL88
DavidL88 2021 年 12 月 14 日
回答済み: Image Analyst 2021 年 12 月 14 日
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 日
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 件のコメント
DavidL88
DavidL88 2021 年 12 月 14 日
That worked thanks.
Steven Lord
Steven Lord 2021 年 12 月 14 日
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 日
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);

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by