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

 Respuesta aceptada

Stephen23
Stephen23 el 5 de Mzo. de 2026 a las 10:48
Editada: Stephen23 el 5 de Mzo. de 2026 a las 11:05
"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 comentarios

Jason
Jason el 5 de Mzo. de 2026 a las 11:54
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 el 5 de Mzo. de 2026 a las 17:19
Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Printing and Saving en Centro de ayuda y File Exchange.

Productos

Versión

R2024b

Etiquetas

Preguntada:

el 5 de Mzo. de 2026 a las 9:06

Comentada:

el 5 de Mzo. de 2026 a las 17:19

Community Treasure Hunt

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

Start Hunting!

Translated by