Fopen returning "Unable to find file. Ensure file exists and path is valid"
8 views (last 30 days)
Show older comments
Caleb Walker
on 28 Apr 2022
Commented: Star Strider
on 28 Apr 2022
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.
0 Comments
Accepted Answer
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"
Make appropriate changes to get the desired format for ‘printfile’.
.
4 Comments
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.)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!