当文件名包含 Unicode 字符时使用 saveas 保存图形
Saving a figure using saveas when filename contains a Unicode character
总结:
我想保存一个文件名为 σ=0.25.fig
的图形,但保存失败。
阐述:
我正在使用 R2019b 更新 3(在 Win10 上),我无法修改我的 MATLAB 配置以允许保存其中包含 Unicode 字符的 .m
文件(即 feature('DefaultCharacterSet')
卡在 'US-ASCII'
上) ,我不得不使用 sprintf
来在我的源文件中存储各种非 ASCII 符号。这通常效果很好,但显然在尝试使用 saveas
.
时会导致问题
示例:
考虑以下代码,
hF = figure();
saveas( hF, sprintf('\x03C3=%4.2f.fig', 0.25) ) % sprintf correctly resolves to "σ=0.25.fig"
哪个(在我的系统上?)导致以下错误:
Error using save
Unable to write to MAT-file σ=0.25.fig.
The file may be corrupt.
Error in matlab.graphics.internal.figfile.FigFile/write (line 32)
save(obj.Path, obj.MatVersion, '-struct', 'SaveVars');
Error in savefig (line 83)
FF.write();
Error in saveasfig (line 6)
savefig(h, name);
Error in saveas (line 153)
feval( ['saveas' format], h, name )
问题:
鉴于上述错误,如何使用所需的文件名保存图形?
幸运的是,用于移动或重命名文件的 movefile
函数不会遇到与 Unicode 路径相同的问题。因此,我们所需要的只是将保存分为两步,首先使用临时 path/name(由 saveas
"likes" 组成的字符),然后使用 moving/renaming所需的 name/path:
saveas(hF, 'tmp.fig');
movefile('tmp.fig', fullfile(pwd, sprintf('\x03C3=%4.2f.fig', 0.25)));
...产生预期的结果,
总结:
我想保存一个文件名为 σ=0.25.fig
的图形,但保存失败。
阐述:
我正在使用 R2019b 更新 3(在 Win10 上),我无法修改我的 MATLAB 配置以允许保存其中包含 Unicode 字符的 .m
文件(即 feature('DefaultCharacterSet')
卡在 'US-ASCII'
上) ,我不得不使用 sprintf
来在我的源文件中存储各种非 ASCII 符号。这通常效果很好,但显然在尝试使用 saveas
.
示例: 考虑以下代码,
hF = figure();
saveas( hF, sprintf('\x03C3=%4.2f.fig', 0.25) ) % sprintf correctly resolves to "σ=0.25.fig"
哪个(在我的系统上?)导致以下错误:
Error using save
Unable to write to MAT-file σ=0.25.fig.
The file may be corrupt.
Error in matlab.graphics.internal.figfile.FigFile/write (line 32)
save(obj.Path, obj.MatVersion, '-struct', 'SaveVars');
Error in savefig (line 83)
FF.write();
Error in saveasfig (line 6)
savefig(h, name);
Error in saveas (line 153)
feval( ['saveas' format], h, name )
问题: 鉴于上述错误,如何使用所需的文件名保存图形?
幸运的是,用于移动或重命名文件的 movefile
函数不会遇到与 Unicode 路径相同的问题。因此,我们所需要的只是将保存分为两步,首先使用临时 path/name(由 saveas
"likes" 组成的字符),然后使用 moving/renaming所需的 name/path:
saveas(hF, 'tmp.fig');
movefile('tmp.fig', fullfile(pwd, sprintf('\x03C3=%4.2f.fig', 0.25)));
...产生预期的结果,