Error Saving Fig with complicated filename

Hello, is there a reason why a matlab figure won't save when the filepath is like this:
completepath =
'F:\IMAGES\2026\Mar\05\08_TestData-2100\05-Mar-2026_08:57:24_Starting_ZTT_-2100.fig'
saveas(F1,completepath); % call save as function to save the file
I get this:
Error using save
Unable to open file "F:\IMAGES\2026\Mar\05\08_TestData-2100\05-Mar-2026_08:57:24_Starting_ZTT_-2100.fig" for output.
Error in matlab.graphics.internal.figfile.FigFile/write (line 33)
save(obj.Path, obj.MatVersion, '-struct', 'SaveVars');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in savefig (line 33)
FF.write();
^^^^^^^^^^

 采纳的回答

"is there a reason why a matlab figure won't save when the filepath is like this"
Yes, because colons are not valid in MS Windows filenames:
'F:\IMAGES\2026\Mar\05\08_TestData-2100\05-Mar-2026_08:57:24_Starting_ZTT_-2100.fig'
^ ^
In Windows path syntax, the colon is reserved for:
  1. Drive letter specification e.g. "F:\..."
  2. Alternate Data Streams (ADS) syntax : "filename:streamname"
Allowing : in normal filenames would conflict with this parsing. Windows prohibits the following characters:
< > : " / \ | ? *
So the two colons in "08:57:24" make the filename invalid.

3 个评论

thanks for pointing that out!
FYI if you're using datetime to generate the date-and-time part of those filenames, you can change the datetime's Format property to avoid the colons.
d = datetime('now')
d = datetime
05-Mar-2026 15:05:54
formatWithColon = d.Format
formatWithColon = 'dd-MMM-uuuu HH:mm:ss'
d.Format = replace(d.Format, ':', '_')
d = datetime
05-Mar-2026 15_05_54
formatWithoutColon = d.Format
formatWithoutColon = 'dd-MMM-uuuu HH_mm_ss'
Or you might want to convert the datetime to a string then do the processing on that string.
d2 = datetime('now')
d2 = datetime
05-Mar-2026 15:05:54
s = string(d2)
s = "05-Mar-2026 15:05:54"
s = replace(s, ":", "_")
s = "05-Mar-2026 15_05_54"

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Printing and Saving 的更多信息

产品

版本

R2024b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by