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 il y a environ 11 heures
Modifié(e) : Stephen23 il y a environ 10 heures
"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 il y a environ 10 heures
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 il y a environ 4 heures
Thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2024b

Tags

Question posée :

il y a environ 18 heures

Commenté :

il y a environ 9 heures

Community Treasure Hunt

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

Start Hunting!

Translated by