当文件名包含Unicode字符时,使用saveas保存图形

问题描述 投票:0回答:1

摘要:我想使用诸如σ=0.25.fig的文件名保存图形,但是保存失败。

详细说明:我正在使用R2019b Update 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 ) 

问题:鉴于上述错误,如何用所需的文件名保存图形?

matlab unicode save filenames matlab-figure
1个回答
0
投票

幸运的是,用于移动或重命名文件的movefile函数不会遇到相同的Unicode路径问题。因此,我们所需要做的就是将保存分为两个步骤,首先使用临时路径/名称(由movefile“喜欢”的字符组成),然后移动/重新命名为所需的名称/路径:

saveas

...产生期望的结果,

saveas(hF, 'tmp.fig'); movefile('tmp.fig', fullfile(pwd, sprintf('\x03C3=%4.2f.fig', 0.25)));

© www.soinside.com 2019 - 2024. All rights reserved.