how to save current date and time to a text file

51 ビュー (過去 30 日間)
Muhammad Haziq
Muhammad Haziq 2018 年 11 月 28 日
コメント済み: Rik 2018 年 11 月 28 日
Hi,
I am trying to save current date and time in .txt file through MATLAB fopen fclose command but it gıves error. can anybody help me here is my syntax.
close all
clear all
time = datestr(clock,'YYYY/mm/dd HH:MM:SS:FFF');
fileID = fopen('trialt.txt');
fprintf(fileID,'%23s\n','time');
fclose('trial');

採用された回答

Muhammad Haziq
Muhammad Haziq 2018 年 11 月 28 日
Hi,
Thank you for guiding me I resolved this issue.
  1 件のコメント
Rik
Rik 2018 年 11 月 28 日
Glad to be of help.
Are you aware you posted your comment as an answer and marked it as accepted answer?

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2018 年 11 月 28 日
編集済み: Rik 2018 年 11 月 28 日
There are three main issues here: you are printing the literal string 'time' to the file, instead of the contents of the time variable. The second issue is that you should use the fileID as input to fclose. The third issue is that you didn't specify the write permission in fopen, so it didn't generate a valid identifier.
Also, you don't need close all here, nor clear all. The later should never be used anyway. In a debugging context you can consider using clearvars instead.
The code below should do what you need.
close all;clearvars%neither is needed here
time = datestr(clock,'YYYY/mm/dd HH:MM:SS:FFF');
fileID = fopen('trialt.txt','wt');
fprintf(fileID,'%23s\n',time);
fclose(fileID);

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by