matlab如何利用​函数自动保存图像并以​时间命名yyyy-m​m-dd-hhmms​s?

1 vue (au cours des 30 derniers jours)
Ping Chen
Ping Chen le 29 Déc 2021
Réponse apportée : Lokesh le 11 Oct 2023
matlab如何利用函数自动保存图像并以时间命名yyyy-mm-dd-hhmmss?
以保存时的时间为名称,例如2021-12-29-100530

Réponses (1)

Lokesh
Lokesh le 11 Oct 2023
Hi Ping,
I understand that you want to save images with a filename that includes the current timestamp in the format "yyyy-mm-dd-hhmmss".
To achieve this, we can create a MATLAB function that generates the timestamp, constructs the filename using the timestamp, and saves the image with the generated filename.
Here is an example MATLAB function that demonstrates the suggested workaround:
function saveImageWithTimestamp(image)
% Generate the timestamp in the desired format
dt = datetime('now', 'Format', 'yyyy-MM-dd-HHmmss');
% Convert the datetime object to a string
timestamp = char(dt);
% Create the filename using the timestamp
filename = strcat(timestamp, '.jpg');
% Save the image with the generated filename
imwrite(image, filename);
% Display a message indicating the successful save
fprintf('Image saved as %s\n', filename);
end
To use this function, we should pass the image as an argument when calling saveImageWithTimestamp, like this:
image = imread('your_image.jpg');
saveImageWithTimestamp(image);
Please go through the following MATLAB documentation links to know more about the functions used above:
I hope you find this helpful.
Best Regards,
Lokesh

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!