0

I would like to save all simulation variables ad fig with the current time.

my solution:

t = datetime('now','Format','dd-MM-yyyy''_T''HHmmss');
t2 = datevec(t);
DateString = datestr(t2);
filename=[DateString,' all_variables_main '];
save(filename )
savefig(filename)

The following error was given in Matlab:

Unable to write file 26-Oct-2019 09:47:15 all_variables_main : Invalid argument.

What have I done wrong?

2 Answers2

1

File name containing : character is not a valid file name.

You can replace the : with "꞉" character.
See: How to get a file in Windows with a colon in the filename?

You can replace all : with character (unicode character A789 that looks like colon) that is valid to be used in file name.

filename(filename == ':') = char(hex2dec('A789'));

Make sure to use the right character when loading the file.

Remark: The above solution was tested in Windows 10, and MATLAB R2016a.

Rotem
  • 30,366
  • 4
  • 32
  • 65
1

mat filenames cannot have spaces or colons in them. You can use the following to directly obtain the date and time in a format that is allowed in a filename:

>> fileName = [datestr(now, 'dd-mmm-yyyy_HHMMSS') '_all_variables_main']
fileName =
    '26-Oct-2019_103123_all_variables_main'
>> save(fileName)
rinkert
  • 6,593
  • 2
  • 12
  • 31