Fopen returning "Unable to find file. Ensure file exists and path is valid"

8 views (last 30 days)
I am trying to print a file with the data and time it was written.
currentime = now; %gets current time in numeric value
currentdate = datestr(datetime(currentime,'ConvertFrom','datenum')); %converst time to day/month/year format
printfile = "Milestone_5_results_date.txt";
printfile = strrep(printfile,'date',currentdate);
fileID = fopen(printfile, 'a');
If I get rid of the last printfile line the code outputs to Milestone_5_results_date.txt just fine, but when I try to add the data in place of the string it breaks the code? I am very confused on how the name of the file makes it not be able to be open.

Accepted Answer

Star Strider
Star Strider on 28 Apr 2022
I am not certain what the problem is.
Try this —
currentdate = string(datetime('now', 'Format','dd/MM/yyyy')); %converst time to day/month/year format
printfile = "Milestone_5_results_"+currentdate+".txt"
printfile = "Milestone_5_results_28/04/2022.txt"
Make appropriate changes to get the desired format for ‘printfile’.
.
  4 Comments
Star Strider
Star Strider on 28 Apr 2022
As always, my pleasure!
Not stupid at all! That’s how we learn. (I didn’t originally see it as a problem and only thought of that possibility later.)

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!