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();
^^^^^^^^^^

 採用された回答

Stephen23
Stephen23 約16時間 前
編集済み: Stephen23 約15時間 前

1 投票

"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 件のコメント

Jason
Jason 約15時間 前
thanks for pointing that out!
Steven Lord
Steven Lord 約11時間 前
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"
Jason
Jason 約9時間 前
Thanks

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2024b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by