MATLAB: Save file with timestamp in a deployed application

matlab coder

I would like to save a textfile with a timestamp as filename, but the function datetime() is not supported for C/C++ code generation with MATLAB Coder.
Is there any other way to save a TXT file with a timestamp as filename? For example:
2019-4-29_14_12_36.txt

Best Answer

If your target supports the time.h library, you could use the "strftime" function:
So a MATLAB code example would be:
function main
if coder.target('MATLAB')
%...

% code for execution in MATLAB
%...
else
rt = coder.opaque('time_t','HeaderFile','time.h');
lt = coder.opaque('struct tm*','HeaderFile','time.h');
coder.ceval('time',coder.wref(rt));
lt = coder.ceval('localtime',coder.rref(rt));
timestamp = zeros(1,256,'int8');
coder.ceval('strftime',coder.wref(timestamp),int32(length(timestamp)),['%Y-%m-%dT%H_%M_%S' 0], lt);
fid = fopen(char(timestamp),'w');
fclose(fid);
end
end