Name must be a text scalar
Show older comments
Hi,
I wan to use the datetime() function to name a folder. Moreover, I want to change the format with the replace() function and later access this folder via dir(). However, I get an error that says "Name must be a text scalar". I suppose the reason lies in the replace function. Is there any other way to do this?
actual_time = datestr(datetime('now')); % holt die aktuelle Zeit
files = dir([actual_time '\*.txt']); % works
actual_time = fullfile(replace(actual_time, ':', '-'))
files = dir([actual_time '\*.txt']); % error
2 Comments
They both work here (I also replaced deprecated DATESTR with the recommended CHAR).
Note that you should use FULLFILE instead of concatenating text together.
actual_time = char(datetime('now'))
S1 = dir([actual_time '\*.txt']) % Should use FULLFILE here
actual_time = fullfile(replace(actual_time, ':', '-'))
S2 = dir([actual_time '\*.txt']) % Should use FULLFILE here
"Is there any other way to do this?"
Explicitly specify the DATETIME format:
DT = datetime('now','Format','uuuu-MM-dd HH-mm-ss')
S = dir(fullfile(char(DT),'*.txt'))
Marco Pedata
on 7 Jun 2023
Answers (0)
Categories
Find more on Undirected Graphs 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!