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

 Réponse acceptée

Stephen23
Stephen23 le 5 Mar 2026
Modifié(e) : Stephen23 le 5 Mar 2026
"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 commentaires

Jason
Jason le 5 Mar 2026
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"
Jason
Jason le 5 Mar 2026
Thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Produits

Version

R2024b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by